Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an "anonymous" generic tag in C#, like '?' in Java?

In Java, one can declare a variable parameterised by an "unknown" generic type, which looks like this:

Foo<?> x; 

Is there an equivalent construct to this question-mark, in C#?

like image 375
EfForEffort Avatar asked Sep 22 '08 19:09

EfForEffort


People also ask

Can we have generic method in non-generic class in C#?

Yes, There are two level where you can apply generic type . You can apply generic type on Method level as well as Class level (both are optional).

Can a generic type be null?

The definition of a generic type should never be nullable. The nullability should be defined by the one using this generic. With the current Kotlin version, you can have a "String?" implementation and a "String" implementation.

What is generic in C?

Generics is the idea to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes and interfaces. For example, classes like an array, map, etc, which can be used using generics very efficiently.

Can generic class have non-generic method?

Yes, you can define a generic method in a non-generic class in Java.


2 Answers

The short answer is no. There isn't an equivalent feature in C#.

A workaround, from C# from a Java developer's perspective by Dare Obasanjo:

In certain cases, one may need create a method that can operate on data structures containing any type as opposed to those that contain a specific type (e.g. a method to print all the objects in a data structure) while still taking advantage of the benefits of strong typing in generics. The mechanism for specifying this in C# is via a feature called generic type inferencing while in Java this is done using wildcard types. The following code samples show how both approaches lead to the same result.

C# Code

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);      } } 

Java Code

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);      } } 
like image 123
Sergio Acosta Avatar answered Sep 28 '22 00:09

Sergio Acosta


AFAIK you can not do that in C#. What the BCL does and there are plenties of examples there is to create a class that is not generic and then create a generic class that inherits the base behavior from the previous one. See example below.

class Foo { }  class Foo<T> : Foo { } 

The you can write something like this:

Foo t = new Foo<int>(); 
like image 22
Jorge Ferreira Avatar answered Sep 28 '22 02:09

Jorge Ferreira