Static method and variables in c#

Total Post:341

Points:2389
C#  .NET 
C#
 869  View(s)
Ratings:
Rate this:
I want to know what is Static method and variables in c# and how to use please help me.
  1. Post:281

    Points:2003
    Re: Static method and variables in c#

    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();
            }
        }
    }

     

      Modified On Apr-10-2018 06:10:19 AM

Answer