Introduction
Object initializers and Collection initializers are part of C# 3.0.These two concepts add a flexibility, readability, and maintainability in C#. As we are C# developers, we should know how and in which situations we can use these two concepts and inprove our programming logic. Before going deep, dive into object and collection initializers. We will check what are these Syntactic Sugars.
Syntactic sugar in programming Syntactic sugar, or syntax sugar, is considered as a "shortcut" provided by the language, which reduces the amount of code that must be written in a particular situation.
Syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.
The main benifits of using syntatic sugar in programming are-
- Avoid lot of keystrokes,The efficiency of software programs is sometimes measured by the number of keystrokes it requires to write a specific function.
- Easy to readable and maintainable.
- Time saving approach.
Let's see an example of how we can use syntactic sugar in our program. Let's consider an example of normal IF-ELSE conditional statement.
- class Program
- {
-
-
- static void Main(string[] args)
- {
- int x = 20, y = 10;
- if (x>y)
- {
- Console.WriteLine(x);
-
- }
- else
- {
- Console.WriteLine(y);
-
- }
- Console.ReadLine();
-
- }
Now, let's use Ternary operators and see how much key strokes we can prevent using Ternary operator.
- class Program
- {
- static void Main(string[] args)
- {
- int x = 20, y = 10;
- var result = x > y ? x : y;
- Console.WriteLine(result);
- Console.ReadLine();
-
- }
So, in this way, we can say that by using ternary operator, we can achive syntactic sugar in our programming. Now, let's come to our main topic about Object Initialization and Collection Initialization.
So in programming, initialization is the assignment of an initial value for a data object or variable.
Object Initializers
Before going toward object initializers, let us see how we are initialising a object in C# 2.0.
- namespace syntax_sugar
- {
- public class Employee
- {
- public string Name { get; set; }
- public int Id { get; set; }
- public string Department { get; set; }
- public int salary { get; set; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Employee emp=new Employee();
- emp.Name = "Kumar";
- emp.Department = "IT";
- emp.Id = 101;
- emp.salary = 80000;
- Console.ReadLine();
-
- }
- }
Let's see how we can do that using object initializer in C# 3.0.
Write the same program using Object Initializer.
- class Program
- {
- static void Main(string[] args)
- {
- Employee emp = new Employee {Name = "Kisan",Id = 55,Department = "IT",salary = 20000};
-
-
- }
- }
It seems that we have saved a lot of Key strokes using this Object Initializer. So, this is also considered as a "Syntactic Sugar".
Collection Initializers
Collection Initializers are used to initialize a collection in a planned and advance manner to avoid lot of Key strokes.
- namespace syntax_sugar
- {
- public class Employee
- {
- public string Name { get; set; }
- public int Id { get; set; }
- public string Department { get; set; }
- public int salary { get; set; }
- }
- class Program
- {
- static void Main(string[] args)
- {
-
- List<Employee> liemp=new List<Employee>();
- Employee emp = new Employee();
- emp.Name = "Kumar";
- emp.Department = "IT";
- emp.Id = 101;
- emp.salary = 80000;
- liemp.Add(emp);
-
- Employee emp1 = new Employee();
- emp1.Name = "Abdul";
- emp1.Department = "IT";
- emp1.Id = 1010;
- emp1.salary = 80000;
- liemp.Add(emp1);
-
- }
- }
Let's see how we can improve our coading quality using collection Initializers. Consider the following program for reference.
- static void Main(string[] args)
- {
-
- List<Employee> liemp = new List<Employee>
- {
- new Employee {Name = "Kumar",Id = 101,Department = "IT",salary = 80000},
- new Employee {Name = "Abdul",Id = 1010,Department = "IT",salary = 80000}
-
-
- };
-
- }
Similarly, we can initialize for all collection. Let us see how we can initialize a dictionary.
- Dictionary<int,string> dict = new Dictionary<int,string>();
- dict.Add(1,"Mohan");
- dict.Add(2, "Kishor");
- dict.Add(3, "Pankaj");
- dict.Add(4, "Jeetu");
We can write a shortcut initialization as follow.
- Dictionary<int,string> dict = new Dictionary<int,string>
- {
-
- {1,"Mohan" },
- {2,"Kishor" },
- {3,"Pankaj" },
- {4,"Jeetu" }
-
- };
Conclusion Here, our main intention is to make you aware about Collection Initialization and Object Initiallization which are two important concepts discovered in C# 3.0, and let you know how these two important concepts are considered as syntactic sugar in progromming.
Hope you liked this article. If you have any doubts and suggestation, please do comment so that I can update