无法理解什么是“Where T:”在 C# 中

发布于 2024-12-04 17:00:10 字数 263 浏览 0 评论 0原文

 public class TwitterResponse<T>
        where T : Core.ITwitterObject
    {
       // all properties and methods here
    }

有人可以简单地向我解释一下这是什么吗?这里的“where T:Core.ITwitterObject”部分是什么?在 Twitterizer 源代码中看到了这一点。有什么例子可以更好地理解这一点吗?

 public class TwitterResponse<T>
        where T : Core.ITwitterObject
    {
       // all properties and methods here
    }

Can someone explain me what is this in simple terms ? what is "where T :Core.ITwitterObject " part here ? Have seen this in Twitterizer source code. Any examples to better understand this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏の忆 2024-12-11 17:00:10

这意味着 T 必须实现接口 Core.ITwitterObject

如果将未实现此接口的类型 T 传递给泛型,则会发生编译时错误。

此条件允许编译器在 T 实例上调用在 Core.ITwitterObject 中声明的函数。

有关详细信息,请参阅文档

示例:

interface IFoo { void Perform(); }

class FooList<T> where T : IFoo
{
    List<T> foos;
    ...
    void PerformForAll()
    {
        foreach (T foo in foos)
            foo.Perform(); // this line compiles because the compiler knows
                           // that T implements IFoo
    }
}

这比常规方法有一个优势

interface IFoo { void Perform(); }

class FooList
{
    List<IFoo> foos;
    ...
    void PerformForAll()
    {
        foreach (IFoo foo in foos)
            foo.Perform();
    }
    // added example method
    IFoo First { get { return foos[0]; } }
}

,因为像 First 这样的方法会更加类型安全,您不需要从 IFoo 向下转换为 SomeRandomFooImpl< /代码>。

This means that T must implement interface Core.ITwitterObject.

If you pass to the generic a type T not implementing this interface, a compile-time error occurs.

This condition allows the compiler to call the functions which are declared in Core.ITwitterObject on instances of T.

Look at the documentation for more information.

Example:

interface IFoo { void Perform(); }

class FooList<T> where T : IFoo
{
    List<T> foos;
    ...
    void PerformForAll()
    {
        foreach (T foo in foos)
            foo.Perform(); // this line compiles because the compiler knows
                           // that T implements IFoo
    }
}

This has an advantage over the customary

interface IFoo { void Perform(); }

class FooList
{
    List<IFoo> foos;
    ...
    void PerformForAll()
    {
        foreach (IFoo foo in foos)
            foo.Perform();
    }
    // added example method
    IFoo First { get { return foos[0]; } }
}

because the methods like First would be more type-safe, you won't need to downcast from IFoo to SomeRandomFooImpl.

纸伞微斜 2024-12-11 17:00:10

它是对泛型类型 T 的约束。这意味着 T 只能是 Core.ITwitterObject 类型

it is a constrain on the generic type T. It means T can only be a Core.ITwitterObject type

伏妖词 2024-12-11 17:00:10

这是泛型的约束,因此该约束表示它必须实现 Core.ITwitterObject。

http://msdn.microsoft.com/en-us /库/d5x73970%28VS.80%29.aspx

It's a constraint for generics, so the constraint says it must implement Core.ITwitterObject.

http://msdn.microsoft.com/en-us/library/d5x73970%28VS.80%29.aspx

放肆 2024-12-11 17:00:10

where 关键字指定泛型类定义中的 T 可以表示什么类型。在这种情况下,这意味着只能表示 ITwitterObject(大概是一个接口),即您只能使用实现 ITwitterObject 接口的对象。

这里有一个非常清楚的解释。关键摘录:

定义泛型类时,可以对泛型类应用限制
客户端代码可以使用类型参数的类型
实例化你的类。如果客户端代码尝试实例化您的
具有约束不允许的类型的类,结果是
编译时错误。这些限制称为约束。
使用 where 上下文关键字指定约束。

The where keyword specifies what type(s) can be represented by the T in the generic class definition. In this case it means that only ITwitterObject (presumably an interface) can be represented, i.e. that you can only use objects that implement the ITwitterObject interface.

There's a pretty clear explanation here. Key excerpt:

When you define a generic class, you can apply restrictions to the
kinds of types that client code can use for type arguments when it
instantiates your class. If client code attempts to instantiate your
class with a type that is not allowed by a constraint, the result is a
compile-time error. These restrictions are called constraints.
Constraints are specified using the where contextual keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文