News for hackers. Stuff that matters.
Re:Using 'this' incorrectly in C# by brundolf on Friday August 14, 2020 @11:04AM (#24158828)
Everyone's chiming in with "you should always use 'this' because it's more explicit", which I personally tend to agree with. But there is a dimension to this that may not be obvious to people who don't come from Java/C#: these languages don't have non-member functions or variables. So if you're accessing a variable without [obj]. it's either a member of the current object, or it's a local variable in the current function. If you call a function without [obj]. it's unlikely (maybe impossible? I'm not certain) that it isn't a method of the current object. On top of that, by idiom Java and C# code tends to be dramatically more verbose than many other languages. So I could understand the desire to cut that down some when there would also be less ambiguity than in, say, JavaScript.

Edit: @masklinn pointed out the static imports feature, which allows you to import static methods in a such a way that they don't have to be prefixed with the class name when calling them, so I take back what I said about function calls

Re:Using 'this' incorrectly in C# by masklinn on Friday August 14, 2020 @11:09AM (#24158906)
> these languages don't have non-member functions or variables

Not true. Both have static imports. Which means in e.g.

    PI * Pow(Radius, 2);
all three symbols may or may not be part of the current class. In this case, only Radius is, the other two come from `using static System.Math` (it's straight from an official example for the `using static` directive).

Also

> it's either a member of the current object, or it's a local variable in the current function.

So they do have non-member variables, in local variables. Making it clear that something is not a local variable is valuable as it means the variable has a much larger scope than you might expect.

Re:Using 'this' incorrectly in C# by brundolf on Friday August 14, 2020 @11:14AM (#24158963)
I didn't know about static imports. Technically those are still member functions, but you are able to call them without a prefix, so I retract that point.

> So they do have non-member variables, in local variables. Making it clear that something is not a local variable is valuable as it means the variable has a much larger scope than you might expect.

Yes, local variables are non-member variables. I'm just pointing out that people's habits may have been built in JavaScript or the like, where a non-member variable could be:

- a global

- from a closure, anywhere up the tree

- an import from another file

Whether or not the explicitness is still important in C# code, it's at least much more important in JavaScript and Python code (in fact I'm pretty sure both languages require it)

Re:Using 'this' incorrectly in C# by masklinn on Friday August 14, 2020 @02:16PM (#24161333)
> Yes, local variables are non-member variables. I'm just pointing out that people's habits may have been built in JavaScript or the like, where a non-member variable could be:

> - a global

> - from a closure, anywhere up the tree

> - an import from another file

C# has "from a closure, anywhere up the tree" and "an import from an other file" though, the only thing it arguably doesn't have is "a global".

> Whether or not the explicitness is still important in C# code, it's at least much more important in JavaScript and Python code (in fact I'm pretty sure both languages require it)

Technically you can use the `with` statement to make it implicit in javascript.

You should not, but you can.

Re:Using 'this' incorrectly in C# by Bjartr on Friday August 14, 2020 @11:28AM (#24159149)
How common is it for code editing tools to be able to highlight local methods v statically imported ones differently? I know Eclipse does it, but I don't know how prevalent the feature is. e.g. Can Vim do it?
Re:Using 'this' incorrectly in C# by kords on Friday August 14, 2020 @03:50PM (#24162449)
Not sure if Vim can do it, but code can be read outside of editors. Sometimes people review PRs in browsers. I can see why there might be a confusion when using static imports or properties/methods from the base class without the base keyword.
Re:Using 'this' incorrectly in C# by tjalfi on Friday August 14, 2020 @11:29AM (#24159159)
> So if you're accessing a variable without [obj]. it's either a member of the current object, or it's a local variable in the current function.

It could also be a field in the parent class. Protected fields are a bad idea but I’m sure some people use them.

Re:Using 'this' incorrectly in C# by brundolf on Friday August 14, 2020 @11:54AM (#24159545)
A field in the parent class is still a member of the current object, and I'm pretty sure there's no way "this." could make a difference there, right?
Re:Using 'this' incorrectly in C# by tjalfi on Wednesday August 19, 2020 @11:24PM (#24219562)
You're correct that the presence or absence of a "this." shouldn't make a difference.

Having said that, C# also permits you to have fields with the same name in both the parent class and the current object.

This will give you a compiler warning suggesting you use the new keyword, though.

Re:Using 'this' incorrectly in C# by bradjohnson on Saturday August 15, 2020 @03:43AM (#24166850)
In addition to static imports, static fields and functions that belong to the instantiated class are not members of the current object.