I’m coming from a c# background, how do generics look in java compared to c#? (basic usage)
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s a pretty huge question, to be honest – the biggest differences aren’t in syntax, but in behaviour… at which point they’re really, really different.
I would suggest you read through the Sun generics tutorial and Angelika Langer’s Java Generics FAQ. Forget everything you know about generics from C#/.NET first though. In particular, while .NET generic types retain the type argument at execution time, Java generics don’t due to type erasure.
So in other words, in C# you can write:
… you can’t do this in Java 🙁
Additionally, .NET generics can have value type type arguments, whereas Java generics can’t (so you have to use
List<Integer>instead ofList<int>for example).Those are probably the two biggest differences, but it’s well worth trying to learn Java generics from scratch instead of as a “diff” from C#.