繁体   English   中英

定义接口时<>的含义是什么?

[英]What does <> mean when defining an interface?

我学习编写自己的接口,并遇到了MSDN文章“ 接口(C#编程指南) ”。 一切似乎都很好,除了:<T>是什么意思或做什么?

interface IEquatable<T>
{
    bool Equals(T obj);
}

这意味着它是一个通用接口。

你可以创建一个这样的界面:

public interface IMyInterface<T>
{
    T TheThing {get; set;}
}

你可以用各种方式实现它:

public class MyStringClass : IMyInterface<string>
{
    public string TheThing {get; set;}
}

和这样:

public class MyIntClass : IMyInterface<int>
{
    public int TheThing {get; set;}
}

它是一个参数类型意味着你可以重用任何类型的IEquatable ...在“运行时”(但不完全),代替T,你可以使用String,Animal,Dog ecc ......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM