STATIC METHOD AND VARIABLES IN C#

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  .NET 
C#
 584  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. aditya kumar Patel

    Post:254

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

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!