Know the answer? Post it. Somebody with the same question will find it here.
Gnanavel SekarRank #333 · 7.5K reputationPosted Mar 23, 2017, 3:34 AM EDT
✓ Accepted answer+1
@Srikanth Reddy
Can we use this keyword in static method?
The answer is no because static method does not need any object to be called, and this keyword always point to a current object of a class.
simply if there is no object then how the keyword point to any current object so,we cannot use this keyword here.
But there is a situation where we can use this keyword in static method, that is EXTENSION METHOD.
what is extension method?
It is a new approach or concept that has been added in C#3.0 version which allows to add new methods in an existing class without editing the source of the class.
we can use this method mainly to extend the functionality of an existing class.
In extension methods new methods can be added to an existing class without editing the source code of a class, by first defining those methods under a static class and then bind them with the class we require.
3. The first parameter of an extension method must be the name of the class prefixed with this keyword to which the method has to be added, and this parameter is referred as binding parameter which will not be taken into consideration while invoking the method.
To have an object pass itself as a parameter to other methods
To have an object return itself from a method
To declare indexers
To declare extension methods
To pass parameters between constructors
To internally reassign value type (struct) value.
To invoke an extension method on the current instance
To cast itself to another type
To chain constructors defined in the same class
You can avoid the first usage by not having member and local variables with the same name in scope, for example by following common naming conventions and using properties (Pascal case) instead of fields (camel case) to avoid colliding with local variables (also camel case). In C# 3.0 fields can be converted to properties easily by using auto-implemented properties.
Paul RajsRank #2,423 · 582 reputationPosted Mar 22, 2017, 9:16 PM EDT
0
yes of Course we can use this keyword in c#
"this" Keyword in C# The "this" keyword is a special type of reference variable, that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined.
we can use this keyword mainly as creating Constructor in c#
C#
publicEmployee(string name, stringalias) { // Use this to qualify the fields, name and alias: this.name = name; this.alias = alias; }
Sorry friends, My question is about using this keyword inside static methods.
0
Paul RajsRank #2,423 · 582 reputationPosted Mar 22, 2017, 10:09 PM EDT
0
51down voteaccepted
Because this refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just use
Gnanavel SekarRank #333 · 7.5K reputationPosted Mar 23, 2017, 3:34 AM EDT
Gnanavel SekarRank #333 · 7.5K reputationPosted Mar 22, 2017, 10:38 PM EDT
Paul RajsRank #2,423 · 582 reputationPosted Mar 22, 2017, 9:16 PM EDT
public Employee(string name, string alias) { // Use this to qualify the fields, name and alias: this.name = name; this.alias = alias; }Suvendu Shekhar GiriRank #546 · 4.4K reputationPosted Mar 22, 2017, 9:30 PM EDT
Jitendra ShekhawatRank #1,909 · 772 reputationPosted Mar 22, 2017, 9:32 PM EDT
Sreekanth ReddyRank #726 · 3.2K reputationPosted Mar 22, 2017, 9:32 PM EDT
Sreekanth ReddyRank #726 · 3.2K reputationPosted Mar 22, 2017, 9:36 PM EDT
Paul RajsRank #2,423 · 582 reputationPosted Mar 22, 2017, 10:09 PM EDT
Because
thisrefers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just useclass Sub { static int y; public static void foo() { y = 10; } }If you want to make sure you get the static field
yand not some local variable with the same name, use the class name to specify:class Sub { static int y; public static void foo(int y) { Sub.y = y; } }Harisankar AngamuthuRank #2,128 · 670 reputationPosted Mar 23, 2017, 3:15 AM EDT
Sandip G PatilRank #1,083 · 1.8K reputationPosted Mar 23, 2017, 5:20 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts