在.NET中是否有内置的泛型类? [英] Is there a built-in generic class for this in .NET?

查看:146
本文介绍了在.NET中是否有内置的泛型类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类X(Of T)
公开value As T
End Class

请注意, Nullable(Of T)也不是 Tuple(Of T)等于这个类,因为它们不允许你设置成员值。该类应该具有以下属性:


  1. 当作为参数传递时,被调用方法所做的任何更改都会影响发送的对象,不应该像创建内部类型一样创建对象的副本。

  2. 应该允许使用语法
  3. 将成员 value code> x.value =< some value> 其中x是X的对象。


解决方案

不是一个确定的答案,但我会放弃它。



我个人并没有看到这种类型的类之前。即使关于泛型的文档也显示了一个类似于您在示例中创建和使用的示例类: $ b

msdn在.net框架中的泛型。



以下是示例:

 公共类Generic(Of T)
公共字段As T

结束类

....

Public Shared Sub Main()
Dim g As New Generic(Of String)
g.Field =A string
'...
Console.WriteLine(Generic.Field ={0},g.Field)
Console.WriteLine(Generic.Field.GetType()= {0 },g.Field.GetType()。FullName)
End Sub


Is there a class that is precisely equal to this class?

Class X(Of T)
    Public value As T
End Class

Note that neither Nullable(Of T) nor Tuple(Of T) are equal to this class because they do not allow you to set the member value. The class should have the following properties:

  1. When passed as parameter, any changes made by the called method should affect the sent object, i.e. it should not create a copy of object as it does for intrinsic types.
  2. Should allow setting the member value to a value using syntax x.value = <some value> where x is an object of X.

解决方案

Not a conclusive answer, but i'll give it a go.

I personally have not seen this type of class before. Even the documentation on generics shows an example class similar to yours being created and used in the examples:

msdn generics in the .net framework.

Here's the example:

Public Class Generic(Of T)
    Public Field As T

End Class

....

Public Shared Sub Main()
    Dim g As New Generic(Of String)
    g.Field = "A string" 
    '...
    Console.WriteLine("Generic.Field           = ""{0}""", g.Field)
    Console.WriteLine("Generic.Field.GetType() = {0}", g.Field.GetType().FullName)
End Sub

这篇关于在.NET中是否有内置的泛型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆