- MyClass myObject = new MyClass() {MyID = 1, MyName = "John Doe"};
Example
- class Program
- {
- static void Main(string[] args)
- {
- //Object initializer
- MyClass myObject = new MyClass() {MyID = 1, MyName = "John Doe"};
- int id =myObject.GetMyID();
- string name = myObject.GetMyName();
- Console.WriteLine("{0,-3:0}{1:s}",id,name);
- }
- }
- public class MyClass
- {
- private int _myID;
- private string _myName;
- public int MyID { get; set; }
- public string MyName { get; set; }
- public MyClass() { }
- public int GetMyID()
- {
- return MyID;
- }
- public string GetMyName()
- {
- return MyName;
- }
- }