generics - protected static string accessability issue in C# -


i have abstract base class templated.

public abstract class base<t> t : new() {      protected static string magic; } 

i have 2 classes derive base, different values of t.

public class derived1:base<other1> {}  public class derived2:base<other2> {} 

two odd things seem happening. first of all, derived2.magic available within member function of derived1; not expect that. additionally, derived2 has static constructor sets magic; odd part being derived2's static constructor not called when derived1 accesses derived2.magic (which shouldn't allowed).

two questions:

  • why can derived1 access protected static member of derived2?

  • i thought static constructors run before static members accessed, why when access derived2.magic, not seeing it's static constructor called?

both of these seem serious bugs.

just adding note if magic on other hand static protected function, problem not exist, makes whole thing more unclear.

why can derived1 access protected static member of derived2?

magic in common base class, not in derived2. subclass of base<t> see variable of same name, though different variables if t different each derived class.

i thought static constructors run before static members accessed, why when access derived2.magic, not seeing it's static constructor called?

there no declaration derived2.magic, there declaration base<t>.magic. derived1 accessing protected field declared in own base class.

you starting assumption magic declared in derived2. if change code that's case, expectations hold.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -