Skip to main content


Showing posts from February, 2015

C#: Auto-Property Initializer in C# 6.0

Any developer who has worked with C# must have used properties sometimes during development. As you know, Auto-Property is declared with simple get and set (i.e. without any backing field), and can be initialized in the constructor once they are declared.
In C# 6.0 a new features is introduced names as Auto-Property initializer. Auto-Property initializer allows property to be initialized like any other field in the same line where it has been declared.
Let’s see Auto-Property Initializer in action: publicbool UserName { get; set; } = "Sandeep";
Auto-Property initializer in C# 6.0 also allows us to initialize read only Auto-Property in the same line where it has been declared. In older version of C#, we had to use a private set for read only properties, but with C# 6.0, without a private set, you can declare and initialize a property in the same line.
Auto-Property Initializer for read only properties: publicbool UserName { get; } = "Sandeep";

Using LINQ concatenate unique items of two List and Sort them.

In this post I’ll show how you can combine unique items of two List<string> and then Sort them with the help of LINQ.
Let’s take two List<string>contains employee names:
List<string> empNameList1 = newList<string>() { "Sandeep", "Ashwani", "Ashish" , "Saurav" };
List<string> empNameList2 = newList<string>() { "Rahul", "Sachin", "Sandeep", "Yuvraj" };
As you can see one name is common in both employee lists, so let’s write LINQ statement (using the Enumerable.Concat method) to get unique names and then sort them and finally print all the sorted unique names by looping through it.

Different ways of String Reversal in C#

As you may be aware C#'s string class don’t have a Reverse() function by default, so let’s discuss different ways to reverse a given string:
Using manual reversal way: The traditional way is to reverse a string by manually looping through it character by character and creating a new string.
string inputStr = "This is test string"; string outputStr = ""; for (int i = inputStr.Length - 1; i >= 0; i--) {     outputStr += inputStr[i]; }
One thing to remember though, if you are using this approach to reverse a large string then you should use StringBuilder to create output string instead of string because a string instance is immutable and you cannot change it after it was created. Any operation that appears to change the string instead returns a new instance.
Using Array.Reverse(): The second approach we can reverse a string is with the inbuilt Array.Reverse() method of Array class.
string inputStr = "This is test string"; char[] charArray = inputStr.To…

Ordering data by more than one column in LINQ query

In this post I’ll show how you can apply ordering on the multiple columns in LINQ query.
If you know LINQ basics, you must be aware that with the help of .OrderBy(x => x.Columnname) in the LINQ query we can order data of the source collection.
So many of the novice developers use the same function twice as mentioned below and thinks that will do the ordering on the multiple columns.
var students = context.Students                 .OrderBy(x => x.Name)                 .OrderBy(x => x.ClassName);
But that is not the solution and LINQ query always does the order by the column you specified in the last OrderBy() method.
We can do order by on more than one columns using lambda expressions as well as in traditional LINQ query.
Mentioned below are two solutions to achieve using Lambda and traditional LINQ queries:
Using Lambda Expression: Change above mentioned query to following to make it work:
var students = context.Students                 .OrderBy(x => x.Name)                 .ThenBy…