C#中是否存在"匿名"通用标记,如"?" 在Java?

EfF*_*ort 25 c# generics syntax

在Java中,可以声明一个由"未知"泛型类型参数化的变量,如下所示:

Foo<?> x;
Run Code Online (Sandbox Code Playgroud)

在C#中,这个问号是否有一个等价的结构?

Ser*_*sta 31

最简洁的答案是不.C#中没有相同的功能.

从Java开发人员的角度来看,来自C#的解决方法Dare Obasanjo:

在某些情况下,可能需要创建一个方法,该方法可以对包含任何类型的数据结构进行操作,而不是包含特定类型的数据结构(例如,打印数据结构中所有对象的方法),同时仍然可以利用在泛型中打字很强.在C#中指定它的机制是通过称为泛型类型推理的特性,而在Java中,这是使用通配符类型完成的.以下代码示例显示了两种方法如何产生相同的结果.

C#代码

using System;
using System.Collections;
using System.Collections.Generic; 

class Test{

    //Prints the contents of any generic Stack by 
    //using generic type inference 
    public static void PrintStackContents<T>(Stack<T> s){
        while(s.Count != 0){
            Console.WriteLine(s.Pop()); 
        } 
    }

    public static void Main(String[] args){

    Stack<int> s2 = new Stack<int>(); 
    s2.Push(4); 
    s2.Push(5); 
    s2.Push(6); 

    PrintStackContents(s2);     

    Stack<string> s1 = new Stack<string>(); 
    s1.Push("One"); 
    s1.Push("Two"); 
    s1.Push("Three"); 

    PrintStackContents(s1); 
    }
}
Run Code Online (Sandbox Code Playgroud)

Java代码

import java.util.*; 

class Test{

    //Prints the contents of any generic Stack by 
    //specifying wildcard type 
    public static void PrintStackContents(Stack<?> s){
        while(!s.empty()){
            System.out.println(s.pop()); 
        }
    }

    public static void main(String[] args){

    Stack <Integer> s2 = new Stack <Integer>(); 
    s2.push(4); 
    s2.push(5); 
    s2.push(6); 

    PrintStackContents(s2);     

    Stack<String> s1 = new Stack<String>(); 
    s1.push("One"); 
    s1.push("Two"); 
    s1.push("Three");   

    PrintStackContents(s1); 
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这实际上并没有回答这个问题.答案是不".上面演示的技巧演示了C#中Java的捕获转换的解决方法,它与方法调用转换一起使用,但在赋值转换或转换表达式中没有用. (30认同)
  • 事实上,这个答案是我所见过的最长的说"不"的方式:) (8认同)

Jor*_*ira 16

AFAIK你不能用C#做到这一点.BCL做了什么,并且有很多例子可以创建一个非泛型的类,然后创建一个继承前一个基本行为的泛型类.见下面的例子.

class Foo
{
}

class Foo<T> : Foo
{
}
Run Code Online (Sandbox Code Playgroud)

你可以写这样的东西:

Foo t = new Foo<int>();
Run Code Online (Sandbox Code Playgroud)


Dan*_*ani 12

虽然承认不是干净的方法,但使用Foo<object> x也可能是合适的.

  • 那么请你取消我的回答吗? (7认同)
  • 适用于所有内容:基元,类对象,结构,枚举......`List <object> list = new List <object>(); list.Add(1); list.Add(new DateTime()); list.Add(new MyStruct()); list.Add(MyEnum.Enum1);` (5认同)
  • -1:使用`object`将泛型类型限制为具有公共超类`object`的所有类型. (4认同)

Com*_*his 5

C#中没有等效的语法.


Dou*_*ean 5

C#中没有等效项是(相当)事实。可以用作类型或调用方法的静态等效项是完全正确的。为此,请使用Jorge的答案

另一方面,有时您需要等效的想法进行反思,并且那里存在等效的想法。如果你有:

interface IFoo<T>
{
  T Bar(T t, int n);
}
Run Code Online (Sandbox Code Playgroud)

你可以得到一个Type代表IFoo<int>使用typeof(IFoo<int>)。少为人知,和部分回答你的问题,是你还可以得到一个Type代表IFoo<T>使用typeof(IFoo<>)

当您想通过反射使用IFoo<T>某些东西T并且T直到运行时才知道时,这很有用。

Type theInterface = typeof(IFoo<>);
Type theSpecificInterface = theInterface.MakeGenericType(typeof(string));

// theSpecificInterface now holds IFoo<string> even though we may not have known we wanted to use string until runtime

// proceed with reflection as normal, make late bound calls / constructions, emit DynamicMethod code, etc.
Run Code Online (Sandbox Code Playgroud)