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();
}
}
} 以上就是c#对象初始化器的内容,更多相关内容请关注php中文网(www.php.cn)!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号