If you are working on AWS Lambda Dotnet with C# and find out that you are getting something like an error like below. The main reason is that you most likely created the lambda on the console first and then tried to upload the code using dotnet cli e.g dotnet lambda deploy-function “functionName”. In order […]

In C#7 there are Expression Bodied Accessors which allows you to write getter and setters somewhat more functional way like lambda’ish. Let me show you an example. You may remember the old way of writing getter and setters like below.
1 2 3 4 5 6 7 8 9 |
public class Automobile { private string _brand; public string Brand { get { return _brand; } set { _brand = value; } } } |
When using C#7 you can use Expression Bodied Accessors like below which makes the […]

In C#7 there are Digit Separators and Binary Literals which were added. One may wonder what these are? Basically you can replace long number values with underscore (_) such that code readability is improved. Let me give you couple of examples of it to make it clear.
1 2 3 4 5 |
//old way of var myBigNumber = 12345678901234567890; //new way with digital separators var myBigNumberNewWay = 123_456_789_012_345_678_90; |
Neat but it doesn’t end there you […]

So one may ask “What is the advantage of using discards in C#?”, and if you don’t know what discards are you can check out the MS C#7 Documentation about it. Basically discards are variables in simple terms, and the declaration of the variable as a discard is by assigning it the underscore (_) as […]

Some may wonder what is the difference between ValueTuple and Tuple that was released in C# 7, and you might say I have been using Tuples for a while now. Whats the difference? Some of you will remember with code like below that showcase the use of ValueTuple since .NET Framework 4.7
1 2 3 4 5 6 7 |
public Tuple<string, string> GetUserName(int userid) { var firstname = db.GetFirstname(userid); var lastname = db.GetLastname(userid); return new Tuple(firstname, lastname); } |
In order […]

Here is a quick C# tip of counting up values of a Dictionary<TKey, TValue> where the values contains a List<T> using Linq. Lets say you have a Dictionary that contains Dictionary<string, List<Guid>> and you wish to find out how many items of Guid you have. You can easily do it by the Sum Linq aggregate […]

Here is a quick C# tip, when using string compare don’t use
1 2 3 |
var str = "Hello"; if(str.ToLower() === "hello") |
use the Equals and StringComparison.InvariantCultureIgnoreCase enum.
1 2 3 |
var str = "Hello"; if(str.Equals("hello", StringComparison.InvariantCultureIgnoreCase)) |
The thing you need to note is which rules are appropriate current culture, the invariant culture, ordinal or another culture entirely. One can create the compare from StringComparer.Create(culture, true)

Here is another C# tip. Most of the time when you are trying to get a value from dictionary you would try to check if the key exist or not something like.
1 2 3 4 5 6 |
if(dictionary.ContainsKey(key)) { var value - dictionary[key]; //do some stuff } |
But one can use TryGetValue
1 2 3 4 |
if(dictionary.TryGetValue(key, out value)) { //do stuff with value } |
The benefit of using TryGetValue is that the dictionary is synchronized, there is no race condition.