This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class AnonymousExtension | |
{ | |
static void Main(string[] args) | |
{ | |
var person = new { FirstName = "Scott", LastName = "Hunter", Sex = 'M', Age = 25 }; | |
var otherPerson = person.With(new { LastName = "Hanselman" }); | |
Console.WriteLine(otherPerson); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From below CodeProject article | |
// https://www.codeproject.com/Articles/894936/Manipulate-your-expression-trees-with-elegance | |
using System; | |
using System.Linq.Expressions; | |
using System.Reflection; | |
namespace ManipulateExpressionTrees | |
{ | |
public static class ReflectionHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Taken from https://codereview.stackexchange.com/a/133709/209350 | |
var words = | |
Regex.Matches("SmallFireBall", @"([A-Z][a-z]+)") | |
.Cast<Match>() | |
.Select(m => m.Value); | |
var withSpaces = string.Join(" ", words); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using var conn = new SqlConnection(connectionString); | |
var connectionStringInfo = new SqlConnectionStringBuilder(connectionString); | |
var dbName = connectionStringInfo.InitialCatalog; | |
var sql = $"SELECT * FROM {tableName} WHERE 1 = 2"; | |
using var command = new SqlCommand(sql, conn); | |
conn.Open(); | |
using var reader = await command.ExecuteReaderAsync(CommandBehavior.KeyInfo); | |
var columnSchema = reader.GetColumnSchema(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void ToCSV(this DataTable dtDataTable, string strFilePath) | |
{ | |
StreamWriter sw = new StreamWriter(strFilePath, false); | |
//headers | |
for (int i = 0; i < dtDataTable.Columns.Count; i++) | |
{ | |
sw.Write(dtDataTable.Columns[i]); | |
if (i < dtDataTable.Columns.Count - 1) | |
{ | |
sw.Write(","); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void OpenWithDefaultProgram(string path) | |
{ | |
Process fileopener = new Process(); | |
fileopener.StartInfo.FileName = "explorer"; | |
fileopener.StartInfo.Arguments = "\"" + path + "\""; | |
fileopener.Start(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Method | Mean | Error | StdDev | Median | Gen 0 | Gen 1 | Gen 2 | Allocated | | |
|----------------------------------- |-----------:|---------:|---------:|-----------:|------:|------:|------:|-------------:| | |
| NoDuplicate | 1,178.0 ms | 23.31 ms | 49.17 ms | 1,154.3 ms | - | - | - | - | | |
| DuplicateInTheMiddle | 556.9 ms | 11.49 ms | 14.53 ms | 558.1 ms | - | - | - | - | | |
| NoDuplicateWithDictionary | 1,235.6 ms | 22.21 ms | 18.54 ms | 1,236.3 ms | - | - | - | 1000000024 B | | |
| DuplicateInTheMiddleWithDictionary | 610.1 ms | 11.60 ms | 12.41 ms | 609.6 ms | - | - | - | 1000000024 B | | |
Use number as index | |
| Method | Mean | Error | StdDev | Median | Gen 0 | Gen 1 | Gen 2 | Allocated | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original question described in this video | |
// https://www.youtube.com/watch?v=XSdr_O-XVRQ | |
public int FindFirstDuplicate(int[] numbers) | |
{ | |
for(int i = 0; i < numbers.Length; i++) | |
{ | |
var number = numbers[i]; | |
if (number < 0) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string NamedFormat(string format, params object[] args) | |
{ | |
var newFormat = string.Empty; | |
var paraIndex = 0; | |
var paraNameIndexMap = new Dictionary<string, int>(); | |
for (int i = 0; i < format.Length; i++) | |
{ | |
var ch = format[i]; | |
if (ch == '{') | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string FormatTimeSpan(TimeSpan timeSpan) | |
{ | |
var (minutes, seconds, milliseconds, microseconds) = ( | |
(int)timeSpan.TotalMinutes, | |
timeSpan.Seconds, | |
timeSpan.Milliseconds, | |
int.Parse(timeSpan.ToString("ffffff").Substring(3)) | |
); | |
return $"{minutes}m {seconds}s {milliseconds}ms {microseconds}μs"; |
NewerOlder