17
Reply

Can “this” be used within a static method?

Princy Gupta

Princy Gupta

May 08, 2014
9.1k
0

    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

    saurabh mittal
    May 09, 2014
    2

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

    Dipak Tipale
    June 14, 2017
    0

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

    Debendra Dash
    June 17, 2016
    0

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

    Rahul Prajapat
    June 01, 2015
    0

    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.

    Debendra Dash
    April 19, 2015
    0

    no, this can not be used in static method

    Safayat Zisan
    April 18, 2015
    0

    yes

    Kml Surani
    April 15, 2015
    0

    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

    Mohit Kumar
    September 06, 2014
    0

    no we cannt use this keyword in static methods

    srinivas m
    August 08, 2014
    0

    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.

    Manju lata Yadav
    June 30, 2014
    0

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

    vkkumar kumar
    May 28, 2014
    0

    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.

    Munesh Sharma
    May 21, 2014
    0

    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.

    Swaraj Patil
    May 20, 2014
    0

    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.

    Prem Ranjan
    May 19, 2014
    0

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

    Pankaj Bajaj
    May 13, 2014
    0

    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

    Khan Abrar Ahmed
    May 12, 2014
    0