一. 初始化器的简单使用
using System;
namespace 初始化器
{
class Program
{
static void Main(string[] args)
{
//1. 常见的初始化方式,将类实例化,并通过构造函数将参数传递进类中,此方式通过 public StudentName(string first, string last)生效
var student1 = new StudentName("Meimei", "Wang");
//2. 没有小括号,直接通过大括号调用类的属性,并将赋值,此即为对象的初始化器,此方式通过 public StudentName() { }
var student2 = new StudentName { FirstName = "Meimei", LastName = "Wang" };
//3. 初始化器也就是默认的构造函数,因而可以用来对任意公共属性进行操作,而指定的构造函数指定按照类中指定的结构进行操作
var student3 = new StudentName { ID = 5 }; //student3 = new StudentName(100) 这是错误的
//4. 另外构造函数和初始化器可以一起使用
var student4 = new StudentName("Lei", "Li") { ID = 122 };
Console.WriteLine(student1.ToString());
Console.WriteLine(student2.ToString());
Console.WriteLine(student3.ToString());
Console.WriteLine(student4.ToString());
Console.ReadKey();
}
}
public class StudentName
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public StudentName() { } //初始化器,其实也就是默认的构造函数
public StudentName(string first, string last) //该类构造函数
{
FirstName = first;
LastName = last;
}
public override string ToString()
{
return FirstName + " " + ID;
}
}
}
二、匿名类的初始化器的使用
using System;
using System.Collections.Generic;
using System.Linq;
namespace 初始化器
{
class Program
{
static void Main(string[] args)
{
var pet = new { Age = 10, Name = "Miaomiao" };
//匿名类中的属性都是只读的
//pet.Name = 100; 此处编译会出错
//匿名类初始化器常用语LINQ语句中
var students = new List<StudentName> { new StudentName("Li", "LI"), new StudentName("Mei", "MEI") };
var studentsFrom = new List<StudentFrom>
{
new StudentFrom { FirstName = "Li", City = "Beijing" },
new StudentFrom { FirstName = "Wang", City = "Shanghai" }
};
var joinQuery = from s in students
join f in studentsFrom on s.FirstName equals f.FirstName
select new { FirstName = s.FirstName, LastName = s.LastName, City = f.City };
foreach(var i in joinQuery)
{
Console.WriteLine("{0} {1} {2}", i.FirstName, i.LastName, i.City);
}
Console.ReadKey();
}
}
public class StudentName
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public StudentName() { } //初始化器,其实也就是默认的构造函数
public StudentName(string first, string last) //该类构造函数
{
FirstName = first;
LastName = last;
}
public override string ToString()
{
return FirstName + " " + ID;
}
}
public class StudentFrom
{
public string FirstName { get; set; }
public string City { get; set; }
}
}
三、集合类初始化器
using System;
using System.Collections.Generic;
using System.Linq;
namespace 初始化器
{
class Program
{
static void Main(string[] args)
{
//集合的初始化器
CollectionInitializer();
Console.ReadKey();
}
private static void CollectionInitializer()
{
var students = new List<StudentName>
{
new StudentName { FirstName = "Mei", LastName = "MEI", ID = 100 }, //使用对象的初始化器创建
new StudentName() { FirstName = "Lei", LastName = "LEI", ID = 101 }, //使用默认的构造函数+初始化器
new StudentName("Li", "LI") { ID = 102 }, //使用指定的构造函数+初始化器
null
};
foreach(var i in students)
{
if (i != null)
{
Console.WriteLine(i.ToString());
}
}
Dictionary<int, StudentName> studentDic = new Dictionary<int, StudentName>
{
{ 111, new StudentName { FirstName = "Mei", LastName = "MEI", ID = 100 } },
{ 112, new StudentName() { FirstName = "Lei", LastName = "LEI", ID = 101 } },
{ 113, new StudentName("Li", "LI") { ID = 102 } }
};
foreach(var s in studentDic)
{
Console.WriteLine(s.Value.ToString());
}
}
}
public class StudentName
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public StudentName() { } //初始化器,其实也就是默认的构造函数
public StudentName(string first, string last) //该类构造函数
{
FirstName = first;
LastName = last;
}
public override string ToString()
{
return FirstName + " " + ID;
}
}
}