Interviews17 answers
Can “this” be used within a static method?
17 Answers
'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 TipalePosted Jun 13, 2017, 11:43 PM EDT
http://www.c-sharpcorner.com/blogs/how-to-use-this-keyword-in-static-method1
Debendra DashPosted Jun 17, 2016, 9:05 AM EDT
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 PrajapatPosted Jun 1, 2015, 6:04 PM EDT
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 DashPosted Apr 18, 2015, 9:57 PM EDT
no, this can not be used in static method
Safayat ZisanPosted Apr 18, 2015, 2:15 PM EDT
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
}
}
Pankaj Kumar ChoudharyPosted Apr 1, 2015, 6:00 PM EDT
No we cannot use this with static variable
For similar kind of
c# interview questions refer this link
Mohit KumarPosted Sep 5, 2014, 9:35 PM EDT
no we cannt use this keyword in static methods
Srinivas MPosted Aug 8, 2014, 9:46 AM EDT
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 YadavPosted Jun 29, 2014, 11:19 PM EDT
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 kumarPosted May 28, 2014, 3:57 AM EDT
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 SharmaPosted May 20, 2014, 10:36 PM EDT
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 PatilPosted May 20, 2014, 12:31 PM EDT
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 RanjanPosted May 19, 2014, 5:40 AM EDT
"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 BajajPosted May 13, 2014, 6:27 PM EDT
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 AhmedPosted May 12, 2014, 4:39 AM EDT
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 mittalPosted May 9, 2014, 3:58 AM EDT











