By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,571 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,571 IT Pros & Developers. It's quick & easy.

Inheritance of static members in the CLR spec vs the C# spec

P: n/a

When i read the CLI spec, 8.10.2 Method inheritance i read the

"A derived object type inherits all of the instance and virtual methods
of its base object type. It does not inherit constructors or
static methods...."

In the C# spec, 17.2.1 Inheritance i read the following:

"A class inherits the members of its direct base class. Inheritance means
that a class implicitly contains all members of its direct base class,
except for the instance constructors, finalizers, and static constructors
of the base class"

Consider the following code:
class A { public static void Method1() { } public static int X = 5;}
class B : A { }

Now i call B.Method1, which is translated to a call A.Method1 in IL.

The C# spec seems to mention that "static" means that members are
related to a specific type, instead of instances...

If C# really inherits all members, wouldn't that mean that the following
code should output 5 instead of the 10?
B.X = 10; Console.WriteLine(A.X);
To come to my question: Am i right when i say that in C# there isn't
true inheritance of static members?

Kind regards,
Tim Van Wassenhove <url:>
Aug 18 '07 #1
Share this Question
Share on Google+
2 Replies

P: n/a
On 2007-08-18, GlennDoten <gd****@gmail.comwrote:

[snip large answer]

Thanks for your answer, i've been thinking it all over (drawing a couple
of venndiagrams and doing some proposition logic) and now i can see how
inheritance in c# maps to inheritance in the cli...
Kind regards,
Tim Van Wassenhove <url:>
Aug 18 '07 #2

P: n/a
GlennDoten <gd****@gmail.comwrote:
Now i call B.Method1, which is translated to a call A.Method1 in IL.

I wouldn't say "translated." The IL emitted by the compiler is what it
is because A.Method1 and B.Method1 are the very same thing because
Method1 is static.

There are two issues here:

1) If the IL call were to B.Method1, that would force type B to be
initialized. By calling A.Method1 instead, type B can remain
uninitialized. This difference has been very significant to some people
in the past.

2) If a new B.Method1 is introduced without recompilation of the
calling code, that new method won't be called with the IL generated by
C#, but would if it generated IL calling B.Method1.

Jon Skeet - <sk***> Blog:
If replying to the group, please do not mail me too
Aug 18 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.