Home > DeveloperSection > Forums > Can “this” be used within a static method in c#
Manoj Bhatt
Manoj Bhatt

Total Post:153

Posted on    January-24-2016 10:50 PM

 C# C#  .NET 

 1 Reply(s)
 421  View(s)
Rate this:
Hi All..
I want to know , can i used 'this' keyword inside a static method.

Thank you.

Shiva Shukla
Shiva Shukla

Total Post:13

Posted on    January-24-2016 11:08 PM

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.
The "this" keyword is a special type of reference variable that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



    class Program


        public class Demo


            int age;

            string name;


            public Demo(int age, string name)


                age = age;

                name = name; 



            public void Show()


                Console.WriteLine("Your age is :" + age.ToString());

                Console.WriteLine("Your name is : " + name);




        static void Main(string[] args)


            int _age;

            string _name;


            Console.WriteLine("Enter your age : " );



            Console.WriteLine("Enter your name : ");



            Demo obj = new Demo(_age, _name);







Output of the above program will be:

Don't want to miss updates? Please click the below button!

Follow MindStick