Princy Gupta
Can “this” be used within a static method?
By Princy Gupta in C# on May 08 2014
  • saurabh mittal
    May, 2014 9

    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

    • 3
  • Dipak Tipale
    Jun, 2017 14

    '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.

    • 0
  • Debendra Dash
    Jun, 2016 17

    http://www.c-sharpcorner.com/blogs/how-to-use-this-keyword-in-static-method1

    • 0
  • Rahul Prajapat
    Jun, 2015 1

    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 } }

    • 0
  • Debendra Dash
    Apr, 2015 19

    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.

    • 0
  • Safayat Zisan
    Apr, 2015 18

    no, this can not be used in static method

    • 0
  • Kml Surani
    Apr, 2015 15

    yes

    • 0
  • Pankaj  Kumar Choudhary
    Apr, 2015 1

    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}}

    • 0
  • Mohit Kumar
    Sep, 2014 6

    No we cannot use this with static variable For similar kind ofc# interview questions refer this link

    • 0
  • Srinivas M
    Aug, 2014 8

    no we cannt use this keyword in static methods

    • 0
  • Manju lata Yadav
    Jun, 2014 30

    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.

    • 0
  • vkkumar kumar
    May, 2014 28

    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:)

    • 0
  • Munesh Sharma
    May, 2014 21

    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.

    • 0
  • Swaraj Patil
    May, 2014 20

    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.

    • 0
  • Prem Ranjan
    May, 2014 19

    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.

    • 0
  • Pankaj Bajaj
    May, 2014 13

    "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.

    • 0
  • Khan Abrar Ahmed
    May, 2014 12

    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

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS