Category: C#

Use https://github.com/Fody/Costura in the past. Nuget install Install-Package Costura.Fody(https://www.nuget.org/packages/Costura.Fody/) Description about what it does from the source: Take all assemblies (and pdbs) that have been marked as “Copy Local” and embed them as resources in the target assembly.

If you can change the definition of class A and class B then you should use one of the other answers here which tells you to add an interface to both those classes. However, if you cannot change the definition of class A and class B (perhaps because they are defined in a library that … Continue reading How to combine two types of C# lists into one?

Compile-time check Recently, Eric Lippert (one of the guys that worked on the C#-compiler while he was at Microsoft)blogged about his top 10 regrets about C#, number four being that In C#, an enum is just a thin type-system wrapper over an underlying integral type. All operations on enums are specified as actually being operations … Continue reading How can I prevent bitwise OR combinations of enum values?

Use ReadAsStringAsync to get the json response… static void Main(string[] args) { HttpClient client = new HttpClient(); Task.Run(async () => { HttpResponseMessage response = await client.GetAsync(“https://maps.googleapis.com/maps/api/geocode/json?address=Los%20Angeles,CA=AIzaSyDABt”); string responseString = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseString); }); Console.ReadLine(); } If you use response.Content.ToString() it is actually converting the datatype of the Content to string so you will get System.Net.Http.StreamContent

Regex is actually faster… but it is likely because you use EndsWith with many OR and redundant ToString(). If you simplify the logic, simple string operation will likely be faster. From the fastest to the slowest (10 millions loop): Fastest Optimized Modded (470 ms) – Gjermund Grøneng Optimized Modded + Constant (597 ms) – Stian … Continue reading Which is faster? Regex or EndsWith?

The difference between ToCharArray and ToArray in C# ToCharArray method: ToCharArray method to extract the characters in a string to a character array. It then displays the original string and the elements in the array. using System; public class Example { public static void Main() { String s = “AaBbCcDd”; var chars = s.ToCharArray(); Console.WriteLine(“Original … Continue reading difference between ToCharArray and ToArray in C#

A shorter notation using Linq’s Range method uses the ability to already figure out the number of days using the TimeSpan.Days property after subtracting start from end. Assuming the start is before end you’d end up with: DateTime StartDate = new DateTime(1979, 10, 4); DateTime EndDate = new DateTime(2016, 10, 4); var dates = Enumerable.Range(0, … Continue reading How to get all dates between two specific dates in C#