110,534
社区成员
发帖
与我相关
我的任务
分享
public class Book
{
public string BookName { get; set; }
public string Author { get; set; }
public double Price { get; set; }
}
public static string GetInfos(Book book, int count)
{
return string.Format("BookName:{0},Author:{1},Price:{2} and Count:{3}",
book.BookName, book.Author, book.Price, count);
}
public static string ChangeInfos(ref Book book, ref int count)
{
book = new Book
{
BookName = "CLR Via C#",
Author = "Jeffrey Richter",
Price = 68.00
};
count = 10;
return string.Format("BookName:{0},Author:{1},Price:{2} and Count:{3}",
book.BookName, book.Author, book.Price, count);
}
book = new Book
{
BookName = "CLR Via C#",
Author = "Jeffrey Richter",
Price = 68.00
};
//http://blog.csdn.net/zztfj/article/details/3746175
C# 3.5 新特性的总结:
一、对象初始化
如果一个类有public字段,在建立类的对象实例时可以使用下面的代码来初始化这些字段;
public class MyClass
{
public String field1;
public int field2;
public bool field3;
}
MyClass my = new MyClass();
my.field1 = “abcd”;
my.field2 = 44;
my.field3 = true;
在C#3.0中提供了一种更简便的方法来初始化这些public变量,代码如下:
MyClass my = new MyClass
{
field1 = “abcd”,
field2 = 44;
field3 =true;
};