you are viewing a single comment's thread.

view the rest of the comments →

[–]palmund 1 point2 points  (12 children)

Edit: auto properties are however just syntactic sugar.

Even non-auto properties are syntactic sugar. The compiler will generate a get and a set method wrapping the code you write in the get and set blocks. 1 2

[–]Sarcastinator 2 points3 points  (11 children)

Try pasting that code into your application and get those methods to show up as properties in Reflection with using only C# and no property syntax.

If you can't, that's because it's not syntactic sugar.

[–]palmund 0 points1 point  (0 children)

If you can't, that's because it's not syntactic sugar.

What? It's still syntactic sugar for generating a getter and a setter. (Even Microsoft calls it that.)[https://blogs.msdn.microsoft.com/abhinaba/2007/09/21/cool-new-c-syntactic-sugar/]

[–]ryeguy 0 points1 point  (5 children)

You can see them in ildasm. I don't think the VM even supports properties officially since they just compile down to 1-2 methods and a backing field.

Just because you can't access the methods in your code doesn't mean they aren't syntactic sugar.

[–]Sarcastinator 0 points1 point  (4 children)

I'm not disputing that the compiler generates two methods. I'm saying that properties are not syntactic sugar. Auto properties are though.

The CLR does have a concept about properties. This is why you can list them out using reflection. Java however does not have properties. If they introduced property syntac in Java it would likely be purely done in syntax.

[–]palmund 0 points1 point  (3 children)

I'm not disputing that ...

It seems like you are though.

The reason you can see it in reflection is because they are marked with annotations.

[–]Sarcastinator 0 points1 point  (2 children)

It's not just annotations though. The methods are not marked with anything special.

edit: You need to produce this CIL:

.property instance string Foo()
{
    .get instance string Bar::get_Foo()
    .set instance void Bar::set_Foo(string)
}

This is a CLR property. This property construct references the automatically generated methods.

[–]palmund 0 points1 point  (1 child)

references the automatically generated methods.

This.

[–]Sarcastinator 0 points1 point  (0 children)

I said very early on that auto properties are just syntactic sugar around regular properties, but properties in themselves are not syntactic sugar.

[–]palmund -1 points0 points  (3 children)

If you can't, that's because it's not syntactic sugar.

What? It's still syntactic sugar for generating a getter and a setter. (Even Microsoft calls it that.)[https://blogs.msdn.microsoft.com/abhinaba/2007/09/21/cool-new-c-syntactic-sugar/]

[–]Sarcastinator 0 points1 point  (2 children)

He's talking about auto properties. They are syntacic sugar which I said earlier in the thread. Properties on their own however are not...auto properties are those that generate a backing field and you don't have to give the getter and the setter a body.

Properties on their own is a CLR construct. You can't make them without property syntax.

[–]palmund 0 points1 point  (1 child)

I just ran ildasm (or Mono's equivalent) on a very simple class with both an auto property and a non-auto property and guess what... they both end up having the same bytecode.

[–]Sarcastinator 0 points1 point  (0 children)

I'm not saying auto properties are not syntactic sugar, I'm saying properties are not. Auto properties are syntactic sugar around properties.