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(); } } }
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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.