Object initializer is very useful to assign value without using constructor or method of the class. You can assign value to property of class using object initializer.
It is reduce code time.
Syntax:
using System; class Program { static void Main() { //Object intializer without any constructor of class myClass obj = new myClass() { Id = 1, Name = "Sample"}; } } class myClass { public int Id { get; set; } public string Name { get; set; } }
When you declare object that time you can assign value to the class properties in between opening “{“and closing parenthesis “}”.
Typically assign value without object initializer: using object of the class.
using System; class Program { static void Main() { //typical assign value using object. myClass obj = new myClass(); obj.Id = 1; obj.Name = "Sample"; } } class myClass { public int Id { get; set; } public string Name { get; set; } }
you can also visit: www.mybook-lang.com
No comments:
Post a Comment