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(); } } }
Liked By
Write Answer
Static method and variables in c#
Join MindStick Community
You have need login or register for voting of answers or question.
Anonymous User
25-Nov-2015Whenever 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.