0

This keyword in a static class ?!

why I can't use (this) keyword in a static class in C#

8th Jun 2017, 11:10 PM
HEROIC
HEROIC - avatar
4 ответов
+ 4
A static class is a sealed class and cannot be instatiated. In other words you can't create a "new" instance of the class and store it in a variable. The this keyword, as mentioned already, is used to refer to the current instance/object of the class. So using the this keyword wouldn't make sense in a static class as it can't be instantiated. If you find that you need to refer to the class in something like an anonymous class or an inner class etc, use the name of the class itself instead of this to access the classes members.
9th Jun 2017, 1:38 AM
ChaoticDawg
ChaoticDawg - avatar
+ 8
what does 'this' keyword do? It refers to the current instance of the object. Static implies the member is a part of the class, not an instance of the object. So saying 'this' in something that's static doesn't really make sense cause static isn't part of the object instance.
8th Jun 2017, 11:51 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Thanks a lot Restoring faith & ChaoticDawg .. well explained :)
9th Jun 2017, 3:34 PM
HEROIC
HEROIC - avatar
0
If I understand your question correctly then C# reserves the word "this" is why you cannot initialize it with that keyword and due to the fact that "this" is static itself.
8th Jun 2017, 11:16 PM
Jonathan
Jonathan - avatar