标签:
自动实现的属性
#region 8-1统计创建了多少个实例的Person类
public class Person
{
public string Name { get; private set; }//声明有公有取值方法的属性
public int Age { get; private set; }
private static int InstanceCounter { get; set; }//声明私有的静态属性和锁
private static readonly object counterLock = new object();
public int InstanceCounterPerson(string name, int age)
{
Name = name;
Age = age;
lock (counterLock)//访问静态属性时使用锁。应避免锁定 public 类型,否则实例将超出代码的控制范围
{
InstanceCounter++;
}
return InstanceCounter;
}
}
#endregion
#region 写一个有单个整数属性的结构
public struct Foo
{
public int Value { get; private set; }//所有字段在被设置之前,不能使用这些属性
public Foo(int value)
: this()//显示的调用无参构造函数this(),编译器才知道所有字段都被明确的赋值了
{
this.Value = value;//调用无参构造函数,改字段被设为默认值
}
}
#endregion
var argss = Environment.GetCommandLineArgs();//用方法的调用结果来初始化一个变量
#region 8-2一个相当简单的Person类
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
List<Person> friends = new List<Person>();//对象创建时,以留空的方式创建,而不是保留空引用
public List<Person> Friends { get { return friends; } }
Location home = new Location();//对象创建时,以留空的方式创建,而不是保留空引用
public Location Home { get { return home; } }
public Person() { }
public Person(string name)
{
Name = name;
}
}
public class Location
{
public string Country { get; set; }
public string Town { get; set; }
}
#endregion
Person tom1 = new Person();
tom1.Name = "Tom";
tom1.Age = 9;
Person tom2 = new Person { Name = "Tom", Age = 9 };//tom1在IL中
Person tom3 = new Person("Tom");
tom3.Age = 9;
Person person3 = new Person("Tom") { Age = 9 };//tom3在IL中
Person[] family = new Person[]
{
new Person{Name="Holly1",Age=36},
new Person{Name="Holly2",Age=36},
new Person{Name="Holly3",Age=36},
new Person{Name="Holly4",Age=36}
};
Person tom4 = new Person("Tom");//设置一个嵌入对象属性
tom4.Age = 9;
tom4.Home.Country = "CS";
tom4.Home.Town = "WC";
Person tom5 = new Person("Tom") { Age = 9, Home = { Country = "CS", Town = "WC" } };//设置一个嵌入对象属性
Person tom6 = new Person("Tom");//设置一个嵌入对象属性
tom6.Age = 9;
tom6.Home.Country = "CS";
tom6.Home.Town = "WC";
#region 集合初始化程序
var names = new List<string>
{
"Holly","jon","Tom","Robin","William"
};
List<string> names1 = new List<string>();//动态添加,IL中无该处代码
names1.Add("Holly1");
names1.Add("jon1");
names1.Add("Tom1");
names1.Add("Robin1");
names1.Add("William1");
Dictionary<string, int> nameAgeMap = new Dictionary<string, int>//任何实现IEnumerable的类型,只要它为初始化列表中出现的每个元素提供了一个公有Add方法,就可以使用这个特性
{
{"Holly",36},//Add方法被调用3次,如果Add有多个重载版本,那么初始化列表每个不用元素都可以调用不通的重载版本
{"Jon",36},
{"Tom",9}
};
#endregion
#region 8-3使用对象1和集合初始化程序来构建一个“富对象”
Person tom = new Person
{
Name = "Tom",
Age = 9,
Home = { Town = "WC", Country = "CS" },//初始化嵌入对象
Friends = //对象初始化程序来初始化集合
{
new Person{Name="Alberto"},
new Person("MAx"),
new Person{Name="Zak",Age=9},
new Person("Ben"),
new Person("Alice")
{
Age=9,
Home={Town="C",Country="S"}
}
}
};
#endregion
#region 8-4创建具有Name和Age属性的匿名类型对象
var tom = new { Name = "Tom", Age = 9 };//匿名对象初始化程序
Console.WriteLine("{0},{1}", tom.Name, tom.Age);//属性具有和初始化程序中的表达式一样的类型,值在创建匿名对象初始化程序中指定
#endregion
#region 8-5用匿名类型填充数组,并计算总年龄
var family = new[]//使用隐式类型的数组初始化程序
{
new{Name="Holly",Age=36},//使用匿名对象初始化程序
new{Name="Jon",Age=32}//如果生成不同的类型,编译器无法判断声明数组的类型
};
int totalAge = 0;
foreach (var person in family)//对每个人使用隐式类型
{
totalAge += person.Age;
}
Console.WriteLine("Total age:{0}", totalAge);
#endregion
#region 8-6从Person对象转换成一个名字和一个成年标志
List<Person> family = new List<Person>
{
new Person{Name="Holly",Age=36},
new Person{Name="Jon",Age=23},
new Person{Name="Jon",Age=23}
};
var converted = family.ConvertAll(delegate(Person person)
{ return new { person.Name, IsAdult = (person.Age >= 18) }; });//投影初始化程序:如果不指定属性名称,而只指定用于求值的表达式,它就会使用表达式最后一部分作为名称————前提是它只能是一个简单的字段和属性
foreach (var person in converted)
{
Console.WriteLine("{0} is an adult?{1}", person.Name, person.IsAdult);
}
#endregion
标签:
原文地址:http://www.cnblogs.com/Tan-sir/p/5169216.html