ASP.NET Tutorial/LINQ/Generics

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Understanding Generics

   <source lang="csharp">

To use generics, you need to import the System.Collections.Generic namespace. List<string> stuffToBuy = new List<string>(); stuffToBuy.Add("socks"); stuffToBuy.Add("beer"); stuffToBuy.Add("cigars");

Here"s how you would declare the list of strings in VB.NET: Dim stuffToBuy As New List(Of String) stuffToBuy.Add("socks") stuffToBuy.Add("beer") stuffToBuy.Add("cigars")

By taking advantage of collection initializers, declare a strongly typed list of strings in a single line like this (in C#): List<string> stuffToBuy2 = new List<string> {"socks", "beer", "cigars"};</source>