Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:44:09+00:00 2026-06-04T18:44:09+00:00

I have searched about static variables in C#, but I am still not getting

  • 0

I have searched about static variables in C#, but I am still not getting what its use is. Also, if I try to declare the variable inside the method it will not give me the permission to do this. Why?

I have seen some examples about the static variables. I’ve seen that we don’t need to create an instance of the class to access the variable, but that is not enough to understand what its use is and when to use it.

Second thing

class Book
{
    public static int myInt = 0;
}

public class Exercise
{
    static void Main()
    {
        Book book = new Book();

        Console.WriteLine(book.myInt); // Shows error. Why does it show me error?
                                       // Can't I access the static variable 
                                       // by making the instance of a class?

        Console.ReadKey();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:44:10+00:00Added an answer on June 4, 2026 at 6:44 pm

    A static variable shares the value of it among all instances of the class.

    Example without declaring it static:

    public class Variable
    {
        public int i = 5;
        public void test()
        {
            i = i + 5;
            Console.WriteLine(i);
        }
    }
    
    
    public class Exercise
    {
        static void Main()
        {
            Variable var1 = new Variable();
            var1.test();
            Variable var2 = new Variable();
            var2.test();
            Console.ReadKey();
        }
    }
    

    Explanation: If you look at the above example, I just declare the int variable. When I run this code the output will be 10 and 10. Its simple.

    Now let’s look at the static variable here; I am declaring the variable as a static.

    Example with static variable:

    public class Variable
    {
        public static int i = 5;
        public void test()
        {
            i = i + 5;
            Console.WriteLine(i);
        }
    }
    
    
    public class Exercise
    {
        static void Main()
        {
            Variable var1 = new Variable();
            var1.test();
            Variable var2 = new Variable();
            var2.test();
            Console.ReadKey();
        }
    }
    

    Now when I run above code, the output will be 10 and 15. So the static variable value is shared among all instances of that class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched but have not been able to find any information about this.
So I have searched for this but not finding anything about it and if
I have searched a lot about UTF8 decoding, but not found the answer yet.
I have searched the web for hours but I can not find anything about
I have searched a lot about the difference between Website and WebApplication but didn't
I have searched the web but couldn't find any detailed idea about Native OS
I have searched about this matter a lot but, I didn't get a proper
I have searched about difference between DNID and Extension. But I couldn't find any
i have searched and read about issues with psexec.exe from sysinternals not working properly
I have searched but all the questions talk about sorting NSStrings. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.