Can “this” be used within a static method?
Princy Gupta
We can't use this in static method because keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class. for more U can refer to http://msdn.microsoft.com/en-us/library/98f28cdx.aspx
'this' - keyword is used to access the instance of an existing class, but in case of static we can't create a instance so it' not possible to use 'this' keyword for static members.
http://www.c-sharpcorner.com/blogs/how-to-use-this-keyword-in-static-method1
No we can not use this operator in static member because this operator operator work as pointer which hold the address of current object. s static member is not object specified it built single time in any class .class dm { int num; public void call1() { this.num = 12200; }public static void call2() { this // program throw an error } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace string_operation {class Program{public void sum( int x){Console.WriteLine("the sum is:" + x);}static void Main(string[] args){Program p = new Program();p.sum(5);p.sub();Console.ReadLine();}}static class Demo{public static void sub(this Program p){Console.WriteLine("this is sub method");}} }Yes we can use this in static method.
no, this can not be used in static method
yes
No we can not use this operator in static member because this operator operator work as pointer which hold the address of current object. s static member is not object specified it built single time in any class .class dm{int num;public void call1(){this.num = 12200;}public static void call2(){this // program throw an error}}
No we cannot use this with static variable For similar kind ofc# interview questions refer this link
no we cannt use this keyword in static methods
A static method does not operate on a specific instance, and it is a compile-time error to refer to this in a static method. But In case of Extension methods it behaves differently Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier.
I can say "Yes" when you create the Extension method for Existing class, the Extension method should contain the "this" keyword " in the parameter to identify the current object. :) But exactly the answer is "No" when you try to access any instance member with in the static method:)
no we can't use because That's an easy one. The keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class. There is a much more in depth explanation of what static members are and why/when to use them in the MSDN docs.
No, We can not use this in Static method becase this reprent current object. Static can not access to it. Again in static method we can access only static properties and method so access to this will violet that rule as well.
No, we can not used "this" keyword within a static method. because "this" keyword refers to the current instance of the class. Static Member functions do not have a this pointer (current instance). Note - we can also not used "base" keyword within a static method.
"this" keyword is used to access the instance of an existing class, but in case of static we can't create a instance so it' not possible to use "this" keyword for static members.
Please refer below links http://stackoverflow.com/questions/134237/why-cant-you-use-the-keyword-this-in-a-static-method-in-net http://stackoverflow.com/questions/3459282/why-keyword-this-cannot-be-used-in-a-static-method