Home  >  Article  >  Backend Development  >  C# object initializer

C# object initializer

黄舟
黄舟Original
2016-12-27 14:14:382035browse

using System;
using System.Collections.Generic;
using System.Linq;using System.Text; 
namespace 对象初始化器
{    
class Program    
{        
static void Main(string[] args)        
{            
//第二种初始化            
var s1 = new student("张三",23);            
Console.WriteLine(s1.ToString());             
//第一种初始化            
var s2 = new student { name = "李四", age = 34 };           
Console.WriteLine(s2.ToString());             
//第三种            
var s3 = new student("王五",30) { ID=1};        
}    
}    
public class student    
{        
public string name { set; get; }        
public int age { set; get; }        
public int ID { set;get;}         
//第一种构造函数        
public student()        
{        
}         
//第二种构造函数        
public student(string Name, int Age)        
{            
name = Name;            
age = Age;        
}        
//方法重载        
public override string ToString()        
{            
return name + ":" + age.ToString();         
}    
}
}

The above is the content of C# object initializer. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn