C 物件和集合的初始化器

2021-07-10 20:30:57 字數 950 閱讀 6719

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

/// /// 商品名稱

///

public string name

/// /// 商品描述

///

public string description

/// /// 商品**

///

public decimal price

/// /// 商品分類

///

public string category

}class program

;//建立並初始化乙個例項

//集合初始化器的使用

listprolist = new list,

new product ,

new product

};//列印

console.writeline("物件初始化器: ", product.productid, product.name, product.price);

foreach (product p in prolist)

", p.productid, p.name, p.price);

}console.readkey();

/*另外還有一些其它型別也可以使用初始化器,如下:

//陣列使用初始化器

//匿名型別使用初始化器

var books = new ;

//字典型別使用初始化器

dictionaryfruitdic = new dictionary() ,

};*/}}

}

物件初始化器和集合初始化器

c 語言開發團隊在c 3.0中增加了乙個名為 物件初始化器 object initializer 的特性 它能初始化乙個物件中的所有允許訪問的字段和屬性。別以為這和你沒關係。我們先來看乙個你非常熟悉不過的 user operator new user operator.id 1 operator.p...

C 物件和集合初始化器

為了提供更合理的建構函式,我們不得不進行多次建構函式的過載,使得使用者的呼叫更加簡單。但是這樣的排列組合隨著引數的增加成幾何級增長,的確很不方便。現在有了object and collection initializer 物件和集合初始化器 就方便多了。舉個例子 private class cat ...

物件初始化器與集合初始化器

物件初始化器 使用物件初始化器,可以在建立時直接向物件賦值,無需顯示的呼叫物件的建構函式。利用這個技術可發人員可以建立匿名型別,顯著的減少編寫物件初始化語句的 new p 不採用物件初始化器寫的 user new user user.id 1 user.pwd 123 user.name 孫悟空 採...