c# 使用初始化器初始化对象
2015-10-20 10:22
525 查看
class Goods { private string strName; public string Name { get { return strName; } set { strName = value; } } private decimal decPrice; public decimal Price { get { return decPrice; } set { decPrice = value; } } private int intQuantity; public int Quantity { get { return intQuantity; } set { intQuantity = value; } } } class Program { static void Main(string[] args) { var goods = new Goods { Name = "辐照度", Price = 5, Quantity = 3 }; Console.WriteLine("水冷日晒的情况" + goods.Quantity + "温度" + goods.Price + "" + goods.Name); Console.Read(); } }
以上为使用对象初始化器;下面为使用集合初始化器:
class Program { static void Main(string[] args) { //var goods = new Goods //{ // Name = "辐照度", // Price = 5, // Quantity = 3 //}; //Console.WriteLine("水冷日晒的情况" + goods.Quantity + "温度" + goods.Price + "" + goods.Name); //Console.Read(); var list = new List<Goods> { new Goods{Name = "轿车", Price= 10000, Quantity = 2}, new Goods{Name = "电脑",Price = 5000, Quantity = 50}, new Goods {Name = "打印机", Price = 4000, Quantity = 5} }; Console.WriteLine("公司近期打算采购以下商品:"); foreach (var item in list) { Console.WriteLine("价值为{0}的{1},采购数量为{2}", item.Price, item.Name, item.Quantity); } Console.Read(); } }
相关文章推荐
- C#中值类型和引用类型的区别深度分析
- C#窗体穿参
- .net 判断文件夹是否存在,创建文件夹
- (3)C#之ADO.Net 数据集(DataSet)
- c#泛型方法
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法
- 【术】c#字典Dictionary自定义类作为key键
- C# StreamReader的指针问题
- 设计模式----单例模式UML图和代码实现(C#&JAVA)
- C#实现 ffmpeg视频转码、播放
- C#中数组、ArrayList和List三者的区别
- c#中convert和parse的区别个人推荐使用convert多
- C# 文件大小
- (原创)c#学习笔记04--流程控制01--布尔逻辑01--布尔赋值运算符
- 【C#】——委托与事件
- c#webAPI几点注意
- C# 代码转换到Python
- C# 5.0 与 .Net 4.5 学习(二)Deadlocks死锁
- C#生成PDF页脚第几页共几页
- C#命令模式