Consider the code of a console application below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PlayInitializer
{
public class Human
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
Human hu1 = new Human();
hu1.Name = "John";
hu1.Age=39;
Human hu2 = new Human();
hu2.Name = "Simon";
hu2.Age = 32;
}
}
}
Object initializer is a short cut technique where you can initialize the public properties of a class quickly, as shown in the code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PlayInitializer
{
public class Human
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
Human hu1 = new Human { Name = "John",
Age = 39 };
Human hu2 = new Human { Name = "Simon",
Age = 32 };
}
}
}
No comments:
Post a Comment
Comments will appear once they have been approved by the moderator