• c#泛型


    泛型(Generic) 允许您延迟编写类或方法中的编程元素的数据类型的规范,直到实际在程序中使用它的时候。换句话说,泛型允许您编写一个可以与任何数据类型一起工作的类或方法。

    使用泛型是一种增强程序功能的技术,具体表现在以下几个方面:

      它有助于您最大限度地重用代码、保护类型的安全以及提高性能。

      您可以创建泛型集合类。.NET 框架类库在 System.Collections.Generic 命名空间中包含了一些新的泛型集合类。您可以使用这些泛型集合类来替代 System.Collections 中的集合类。

      您可以创建自己的泛型接口、泛型类、泛型方法、泛型事件和泛型委托。

      您可以对泛型类进行约束以访问特定数据类型的方法。

      关于泛型数据类型中使用的类型的信息可在运行时通过使用反射获取。

    泛型可以是委托,也可以是方法

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;
    
    namespace 编码练习
    {
        public class MyGenericeArry<T> {
            private T[] arr { get; set; }
            public MyGenericeArry(int size){
                arr = new T[size];
            }
            public T GetItem(int index) {
                return arr[index];
            }
            public void SetItem(int index,T value) {
                arr[index] = value;
            }
        }
        public class Start {
            public static void Main(string[] args)
            {
                MyGenericeArry<int> intarr = new MyGenericeArry<int>(5);
                for (int i = 0; i < 5; i++)
                {
                    intarr.SetItem(i,i*5);
                }
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(intarr.GetItem(i));
                }
                MyGenericeArry<char> chararr = new MyGenericeArry<char>(5);
                for (int i = 0; i < 5; i++)
                {
                    chararr.SetItem(i, 'a');
                }
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(chararr.GetItem(i)); 
                }
            }
        }
    }
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;
    
    namespace 编码练习
    {
        public class Genswap {
            public static void Swap<T>(ref T a1,ref T a2) {
                T temp ;
                temp = a1;
                a1 = a2;
                a2 = temp;
            }
            public static void Main(string[] args)
            {
                int a, b;
                char c, d;
                a = 10;
                b = 5;
                c = 'a';
                d = 'b';
                Swap<int>(ref a,ref b);
                Swap<char>(ref c, ref d);
                Console.WriteLine(string.Format($"{a}{b}"));
                Console.WriteLine(string.Format($"{c}{d}"));
            }
        }
    }
  • 相关阅读:
    Row size too large. The maximum row size for the used table type 解决
    pandas Series介绍
    Scala核心编程_第08章 面向对象编程(中高级部分)
    mysql增删改字段,重命名替换字段
    python报错ValueError: cannot reindex from a duplicate axis
    pandas DataFrame 数据筛选
    Scala核心编程_第07章 面向对象编程(中级部分)
    Scala核心编程_第06章 面向对象编程(基础部分)
    《The Rise and Fall of Scala》scala兴衰读后感
    信贷业务(Ali)
  • 原文地址:https://www.cnblogs.com/jestin/p/11550160.html
Copyright © 2020-2023  润新知