Home > DeveloperSection > Forums > Static method and variables in c#
Ankit Singh

Total Post:341

Points:2389
Posted on    November-25-2015 10:12 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 328  View(s)
Rate this:
I want to know what is Static method and variables in c# and how to use please help me.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    November-25-2015 10:21 PM

Whenever you write a function or declare a variable, it doesn’t create instance in a memory until you create object of class. But if you declare any function or variable with static modifier, it directly create instance in a memory and acts globally. The static modifier doesn’t reference with any object.

 It is very easy to create static modifier with variables, functions and classes. Just put static keyword before the return data type of method.

using System;
namespace Static_variable_and_method
{
    class StaticNumber
    {
        // Create static variable
        public static string Name;
        //Create static method
        public static void GetName()
        {
            Console.WriteLine("Your name is {0}{1}", "Ms.", Name);
            Console.ReadLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter Your Name\t");
            StaticNumber.Name = Convert.ToString(Console.ReadLine());
            StaticNumber.GetName();
        }
    }
}

 


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

Follow MindStick