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 programmingSyntactic 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.
- class Program
- {
- static void Main(string[] args)
- {
- int x = 20, y = 10;
- if (x>y)
- {
- Console.WriteLine(x);
- }
- else
- {
- Console.WriteLine(y);
- }
- Console.ReadLine();
- }
- 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
- 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};
- }
- }
- 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); //one object is added in the list
- Employee emp1 = new Employee();
- emp1.Name = "Abdul";
- emp1.Department = "IT";
- emp1.Id = 1010;
- emp1.salary = 80000;
- liemp.Add(emp1); //Second object is added in the List
- }
- }
- 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");
- Dictionary<int,string> dict = new Dictionary<int,string>
- {
- {1,"Mohan" },
- {2,"Kishor" },
- {3,"Pankaj" },
- {4,"Jeetu" }
- };
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

Manav PandyaPosted Jun 5, 2017, 1:21 AM
I found it useful and going to start implementing
Manav PandyaPosted Jun 5, 2017, 1:20 AM
Nice share sir ...............