C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
Java
Learn iOS Programming
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
17
Reply
Can “this” be used within a static method?
Princy Gupta
11y
26.7k
1
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
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
11y
3
'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
8y
0
http://www.c-sharpcorner.com/blogs/how-to-use-this-keyword-in-static-method1
Debendra Dash
9y
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
10y
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
10y
0
no, this can not be used in static method
Safayat Zisan
10y
0
yes
Kml Surani
10y
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}}
Pankaj Kumar Choudhary
10y
0
No we cannot use this with static variable For similar kind of
c# interview questions
refer this link
Mohit Kumar
11y
0
no we cannt use this keyword in static methods
Srinivas M
11y
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
11y
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
11y
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
11y
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
11y
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
11y
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
11y
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
11y
0
What are the types of comment in C# with examples?
What are circular references?
Message