zoukankan      html  css  js  c++  java
  • 泛型学习

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的指定推迟到客户端代码声明并实例化该类或方法的时候。例如,通过使用泛型类型参数 T,您可以编写其他客户端代码能够使用的单个类,而不致引入运行时强制转换或装箱操作的成本或风险,
    // Declare the generic class
    public class GenericList<T>
    {
        
    void Add(T input) { }
    }

    class TestGenericList
    {
        
    private class ExampleClass { }
        
    static void Main()
        
    {
            
    // Declare a list of type int
            GenericList<int> list1 = new GenericList<int>();

            
    // Declare a list of type string
            GenericList<string> list2 = new GenericList<string>();

            
    // Declare a list of type ExampleClass
            GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
        }

    }

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace Generics{
     
    class Program{
      
    static void Main(string[] args){
       List
    <Customer> customers = new List<Customer>();
       customers.Add(
    new Customer("Motown-Jobs"));
       customers.Add(
    new Customer("Fatman's"));
       
    foreach (Customer c in customers)
       Console.WriteLine(c.CustomerName);
       Console.ReadLine();
      }

     }

     
    public class Customer{
      
    private string customerName = "";
      
    public string CustomerName{
       
    get return customerName; }
       
    set { customerName = value; }
      }

      
    public Customer(string customerName){
       
    this.customerName = customerName;
      }

     }

    }
  • 相关阅读:
    全选和选项交互
    无法将类型为excel.applicationclass的com 强制转换为接口类型的解决方法[转]
    SilverLight搭建WCF聊天室详细过程[转]
    进程与线程的一个简单解释
    Visiual Studio CLR20r3
    C#注册表操作类--完整优化版
    cmd注册外部命令
    C# Dsofile.dll无法注册运行问题解决
    .net版,微信免充值代金卷业务开通验收代码
    C#十进制与任意进制的转换
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/1218054.html
Copyright © 2011-2022 走看看