How is the Generics syntax in C# compared to Java?

Asked

Viewed 151 times

0

I am a Java developer studying C# and came across the following difference between the two languages:

https://nerdparadise.com/programming/csharpforjavadevs

Generics

Generics are Much Less fiddly in C#. Partially because they are Both a Compile-time and run-time Concept. There is no Diamond syntax (e.g. new Arraylist<>();) in C# which may sound horrific, but it’s really not that bad. As a bonus, Primitives are Valid types for Generics. So you can have a List Instead of an Awkward List that Suddenly implies nullability.

Invoking methods that require Generics as Parameters are done between the method name and the parenthesis as opposed to between the dot and the method name. Basically constructors and methods in C# use the same Convention whereas Java used Different syntax for Generics for the constructor and methods.

I did not understand the last paragraph. What does it refer to? Following is translation attempt:

Invoking generic methods as parameters is done between the method name and parentheses, as opposed to between the point and the method name. Basically constructors and methods in C# use the same convention while Java used different syntax for generic constructors and methods.

  • That? https://answall.com/q/10032/101 and https://answall.com/q/150181/101?

  • In fact the title is poorly formulated, the doubt refers to information present in a specific paragraph, but I do not know if I can answer in a few words. I’ll compare it to the other questions.

  • The paragraph says that in C# you call a generic method like this MeuMetodo<int, string, char>(a, b, c, d)

  • @LINQ Got it! Thanks!

  • @Piovezan I was writing an answer, but I can’t remember what it’s like in Java

  • @LINQ meuMetodo(<List> lista, <Map> mapa) { ... }

  • declares the separate types of arguments?

  • Wow, I traveled in mayonnaise. meuMetodo(T1 lista, Map<T1> mapa)... I think you’re right now... and declare the parametric type T1 next to the class name public class MinhaClasse<T1> { ...

Show 3 more comments

2 answers

1

C# and Java Generics are very similar, but they really differ is in compile time and Runtime, while Java solves generics only at compile time, that is, it does not actually create a type for each Generic, is just a type that the compiler makes sugar syntactic to look Generic, but behind the scenes is just a lot of Casts, anyway, in C#, this is solved at compilation time by creating types, example, List differs from List, and so on, this leads to better performance in relation to generics in C#.

1


The second paragraph is saying you can do the following,

IDataReader dr = conn.ExecuteQuery(query);
while (dr.Read())
{
    int id = DBHelper.Read<int>(dr["id"]);
    ...
}

See what I called T DBHelper.Read<T>(object). I passed the generic type between the method name and the parentheses.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.