C#知识整理-泛型

泛型是为所存储或使用的一个或多个类型具有占位符(类型形参)的类、结构、接口和方法。泛型集合类可以将类型形参用作其存储的对象类型的占位符;类型形参呈现为其字段的类型和其方法的参数类型。 泛型方法可将其类型形参用作其返回值的类型或用作其形参之一的类型。
泛型的优点包括:代码的可重用性增加,类型安全性提高。
泛型方法
先从一个简单的例子开始
两个数字相加,如果不适用泛型,我们需要使用重载的方式定义方法,比如float,double,decimal,int,都需要单独写一个重载
使用泛型只需要定义一个泛型方法,接受对应的形参就可以完成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    internal static class Math
    {
        public static T Add<T>(T a, T b) where T:INumber<T> {
            return a + b;
        }
 
        public static int Add(int a , int b) {  return a + b; }
        public static decimal Add(decimal a, decimal b) { return a + b; }
    }
 
Console.WriteLine($"1+4={Math.Add<int>(1, 4)}");
Console.WriteLine($"1.1+4.5={Math.Add<decimal>(1.1m, 4.5m)}");
Console.WriteLine($"1+4={Math.Add(1, 4)}");
Console.WriteLine($"1.1+4.5={Math.Add(1.1m, 4.5m)}");
/*
* output:
1+4=5
1.1+4.5=5.6
1+4=5
1.1+4.5=5.6
*/

  

泛型类/泛型接口
类和接口也可以是泛型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
internal class ClassA: InterfaceA
{
    public ClassA() { }
 
    public void ShowMessage()
    {
        Console.WriteLine("This is ClassA");
    }
}
internal class ClassA<T> : ClassA,InterfaceA
{
    public T Data;
    public ClassA() { }
    public ClassA(T arg)
    {
        Data = arg;
    }
    public void ShowMessage() {
        Console.WriteLine($"This is Generic ClassA, with data:{Data}");
    }
}
 
internal interface InterfaceA
{
    void ShowMessage();
}
 
ClassA a1=new ClassA();
a1.ShowMessage();
ClassA<int> a2=new ClassA<int>(2);
a2.ShowMessage();
/*
 * output:
    This is ClassA
    This is Generic ClassA, with data:2
 */

  

下面做一个个人觉得比较实际的泛型使用方式
定义一个通用的返回值泛型类,作为一个实例,应该很多项目里都有类似的结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class ReturnResult
{
    public bool Success { get; set; }
    public string Code { get; set; }
    public string Message { get; set; }
    public Exception Exception { get; set; }
 
    public ReturnResult()
    {
        Success=false;
    }
}
 
public class ReturnResult<T> : ReturnResult {
    /// <summary>
    /// default constructor
    /// </summary>
    public ReturnResult() : base()
    {
 
    }
 
    /// <summary>
    /// Data
    /// </summary>
    public T Data { get; set; }
}
 
public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    public override string ToString()
    {
        return $"this is person {Name}, age:{Age}";
    }
}
 
public class PersonService {
    public ReturnResult<Person> GetPerson(string name, int age) {
        var result = new ReturnResult<Person>();
        result.Data= new Person() { Name = name, Age = age };
        result.Success = true;
        return result;
    }
    public ReturnResult<Person> GetPersonFailure()
    {
        var result = new ReturnResult<Person>();
        try {
            throw new Exception("Exception in GetPersonFailure");
        } catch (Exception ex) {
            result.Exception= ex;
        }
        return result;
    }
}
 
var _personService=new PersonService();
var getPersonResult1 = _personService.GetPerson("PersonA", 20);
var getPersonResult2 = _personService.GetPersonFailure();
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(getPersonResult1));
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(getPersonResult2));
/*
 * output:
    {"Data":{"Name":"PersonA","Age":20},"Success":true,"Code":null,"Message":null,"Exception":null}
    {"Data":null,"Success":false,"Code":null,"Message":null,"Exception":{"ClassName":"System.Exception","Message":"Exception in GetPersonFailure","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":"   at Learn.CSharp.Generic.PersonService.GetPersonFailure() in D:\\Learn\\C#Learning\\Learn.CSharp.Basic\\Learn.CSharp.Generic\\ReturnResult.cs:line 59","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2146233088,"Source":"Learn.CSharp.Generic","WatsonBuckets":null}}
 */

  

 
posted @   Terry841119  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示