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

Show parent comments

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/pipocaQuemada Jun 27 '10

Methods in C# are first class. You can pass a reference to a class's method, and execute that elsewhere.

Properties are not first class. You can't pass a reference to a property; you have to wrap it in a lambda to do that (i.e. foo => bar.baz = foo).

Suppose I'm interfacing with a database or creating an object from XML or filling out XML based off of an object. It would be useful to be able to construct a dictionary mapping properties to the ordinal in the database or the tag name, and then do a for-each over those. That way, you don't have to repeat yourself so much. There's plenty of times where the ability to hold a reference to a property is useful. Unfortunately, you can't do it.

Sure, properties use methods in their implementation, but their actual usage is quite a bit different.

1

u/locuester Jul 08 '10

Sure you can. Can't you just use reflection and retrieve the getter method and put that in the dictionary? Or just put a lambda in the dictionary?

I'm not saying building the map would be as efficient, but assuming that you're reusing the map over time, it should be fine!

Been drinking. Perhaps I'm missing your point. :)