-4

I'm wondering I can't reach static class field from instance variable

 class aa { public static string b = "bb"; } Console.WriteLine(aa.b); //fine aa f = new aa(); f.b //error 

Why? Do I make something wrong?

2
  • 1
    I think other languages like Java allow this, but this is not supported in C#. Commented Oct 8, 2018 at 9:39
  • 3
    Yep. static methods and properties can only be accessed from the type not an instance of the class. As such static methods do not have acces to non-static properties or methods Commented Oct 8, 2018 at 9:39

1 Answer 1

0

Outside you can get a static field by ClassName.StaticVariable, but inside the class it is similar to other instance variables. This is because static variables are owned by class, not a specific instance.

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.