0
    class A {
}
class B
{
static A a;
}

What does this mean? dose it mean i can have only instance of class A inside class B?

  • 1
    You should start your adventure with C# on MSDN https://msdn.microsoft.com/en-us/library/98f28cdx.aspx – Kamil Budziewski Apr 14 '16 at 08:14
  • 1
    It means that all your instances of class `B` share the same reference to an instance of class `A`. – MakePeaceGreatAgain Apr 14 '16 at 08:15
  • This kind of question is off-topic on stackoverflow where we try to fix concrete programming issues. You might get some help on fundamental language questions on _programmers_: http://programmers.stackexchange.com/questions/163457/understanding-the-static-keyword – Tim Schmelter Apr 14 '16 at 08:17
  • Possible duplicate of [What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?](http://stackoverflow.com/questions/10795502/what-is-the-use-of-static-variable-in-c-when-to-use-it-why-cant-i-declare-th) – MakePeaceGreatAgain Apr 14 '16 at 08:26

1 Answers1

0

The number of instances of A within B is not determinded by static or any other keyword, but by the definition of the member a within class B. So when a is just one A, there surely is exactly one instance of A per B. However if a is declared as a list you surely have more instances.

Static simply means that your instances of B share the same reference to your instance of A, so with your code you have exactly one instance of A that can be used by any kind of B. Moreover you do not even need an instance of B to access that single instance of A. However it does not mean that only one instance of A exist within your application as other classes might create instances of A as well.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111