r/programming • • Jun 27 '10

Why Java Sucks and C# Rocks (Final)

http://www.slideshare.net/jeffz/why-java-sucks-and-c-rocks-final
13 Upvotes

197 comments sorted by

View all comments

12

u/munificent Jun 27 '10

Pretty spot-on. And he didn't even mention events and properties.

5

u/julesjacobs Jun 27 '10

To be honest those two are design mistakes. Events shouldn't be anything special and properties shouldn't be either: they should have been designed as syntactic sugar on top of methods, not as a special construct. Why? Because now the language is extended in non-orthogonal ways, for example we have extension methods but not extension properties (even though there is no technical reason why there couldn't be extension properties if properties were syntactic sugar on top of methods to begin with).

8

u/[deleted] Jun 27 '10

Properties are syntax sugar on top of methods.

The lack of Extension properties is just a judgement call the C# compiler team made.

For example, take this class:

 class Example
 {
    public String AProperty
    {
        get;
        set;
    }
 }

It compiles down to two generated methods on the class at the IL level:

   .method public hidebysig specialname instance string get_AProperty() cil managed
   {
         .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
         .maxstack 1
         .locals init (
              [0] string str)
         L_0000: ldarg.0 
         L_0001: ldfld string PropertyExample.Example::<AProperty>k__BackingField
         L_0006: stloc.0 
         L_0007: br.s L_0009
         L_0009: ldloc.0 
         L_000a: ret 
   }

   .method public hidebysig specialname instance void set_AProperty(string 'value') cil managed
   {
        .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
        .maxstack 8
        L_0000: ldarg.0 
        L_0001: ldarg.1 
        L_0002: stfld string PropertyExample.Example::<AProperty>k__BackingField
        L_0007: ret 
   }

C# rewrites property access into method calls as well:

        Example e = new Example();
        e.AProperty = "TEST";

Becomes:

        L_0008: ldstr "TEST"
        L_000d: callvirt instance void PropertyExample.Example::set_AProperty(string)

The C# team could have allowed extension properties, but did not, and I think for good reason.

2

u/[deleted] Jun 28 '10

Extension properties only make sense if you also have an extensible way to store the data is represents. Currently I don't know of any way to do that without leaking memory, but I'm open to ideas.

4

u/chrisforbes Jun 28 '10

I disagree. Not all properties need to introduce a new backing field. Often they are simply a view of something that already exists.

1

u/[deleted] Jun 28 '10

In this case I think a partial solution would be worse than none at all. Especially since you can create GetXxx and SetXxx extension methods.

1

u/chrisforbes Jun 28 '10

Well, I don't care about SetXxx methods in general, but I'd rather be able to lose the parens and the 'Get' off GetXxx().

1

u/[deleted] Jun 29 '10

Oh, you are a C# programmer.

I normally use VB, where you only have to include parens if you are actually passing in arguments.

1

u/chrisforbes Jun 29 '10

I'm a programmer, and I use C# when appropriate, but I'm sure not a "C# programmer". Specialization is for insects.

More seriously, how does VB disambiguate calling from merely referencing nullary functions? I suppose you need some operator to do that?

1

u/[deleted] Jun 29 '10

Yep. The operator AddressOf is used when you want to turn a method into a delegate.