How is the syntax of generics in C # compared to Java?

0

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

  

link

     

Generics

     

Generics are much less fiddly in C #. Partially because they are both compile-time and run-time concept. There is no diamond syntax (e.g. new ArrayList

asked by anonymous 18.05.2018 / 20:20

2 answers

1

The second paragraph is saying that you can do the following,

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

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

    
30.06.2018 / 20:55
1

C # and Java generics are very similar, but they differ even at compile time and runtime, while Java solves the generics only at compile time, that is, it does not actually create a type for each generic, it's just a type that the compiler makes syntactic sugar to look generic, but behind the scenes is just a bunch of casts, so in C #, this is solved at compile time by creating types, example, List differs from List, and so on , this leads to better performance compared to generics in C #.

    
30.06.2018 / 20:33