-
java vs c#
Hello,
I've heard that in Java even the methods (of a class) can be generics? Are this correct? Could you give me a simple examples ? ( I come from c++ and this for me sounds strange)...
Furthermore: Does C# permit the same thing? (generic methods) ?
thanks in advance...
-
Sure, and the syntax is nearly identical between Java and C# for this:
Java:
public static <T> short Compare(T Arg1, T Arg2)
C#:
public static short Compare<T>(T Arg1, T Arg2)
You can call it either by explicitly specifying the type or not:
Java:
o.Compare(1, 2);
o.<int>Compare(1, 2);
C#:
o.Compare(1, 2);
o.Compare<int>(1, 2);
-
thanks for reply...and Can these method be virtual too? (I suppose of course for java, being in Java all methods virtual by default)...
-
Yes:
C#:
public virtual short Compare<T>(T Arg1, T Arg2)
And as you said, all methods in Java are virtual without the need for a keyword.
-
furthermore: does C# (and java) permit overloading of methods?
-
Yes. Overloading requires no special syntax in either language - just create a new method with the same name but different signature.
If what you meant was overriding base class methods, then you can use:
C#:
protected override void TestMethod()
{
}
Java:
@Override
protected void TestMethod()
{
}
C# additionally allows shadowing base class methods:
protected new void TestMethod()
{
}
-
ONE BID DIFFERANCE WITH OVERLOADING IN JAVA VS C#.........
You cannot I repeat CANNOT overload operators (this got me in a bind in my first java project). However, is some frameworks and DSLs, like groovy, this functionality is added.
"America... just a nation of two hundred million used car salesmen with all the money we need to buy guns and no qualms about killing anybody else in the world who tries to make us uncomfortable. " - Hunter S. Thompson
MCAD
IBM Business Partner
-
could anyone explain me the sense of the
"Comparing C# and Java Generics", Anders Hejlsberg reply at paragraph 3 ("Issue number two, and.......) ?
http://www.artima.com/intv/generics2.html
I don't understand the sense of what he says about reflection in java (is it possibile or not?)
thanks,
Similar Threads
-
Replies: 9
Last Post: 09-19-2007, 05:58 AM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks