Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The short answer is no. There isn't an equivalent feature in C#.</p> <p>A workaround, from <a href="http://www.25hoursaday.com/CsharpVsJava.html#generics" rel="noreferrer">C# from a Java developer's perspective</a> by Dare Obasanjo:</p> <p>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.</p> <p>C# Code</p> <pre><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&lt;T&gt;(Stack&lt;T&gt; s){ while(s.Count != 0){ Console.WriteLine(s.Pop()); } } public static void Main(String[] args){ Stack&lt;int&gt; s2 = new Stack&lt;int&gt;(); s2.Push(4); s2.Push(5); s2.Push(6); PrintStackContents(s2); Stack&lt;string&gt; s1 = new Stack&lt;string&gt;(); s1.Push("One"); s1.Push("Two"); s1.Push("Three"); PrintStackContents(s1); } } </code></pre> <p>Java Code</p> <pre><code>import java.util.*; class Test{ //Prints the contents of any generic Stack by //specifying wildcard type public static void PrintStackContents(Stack&lt;?&gt; s){ while(!s.empty()){ System.out.println(s.pop()); } } public static void main(String[] args){ Stack &lt;Integer&gt; s2 = new Stack &lt;Integer&gt;(); s2.push(4); s2.push(5); s2.push(6); PrintStackContents(s2); Stack&lt;String&gt; s1 = new Stack&lt;String&gt;(); s1.push("One"); s1.push("Two"); s1.push("Three"); PrintStackContents(s1); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload