473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

Hello,

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

"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:http://www.timvw.be/>
Aug 18 '07 #1
2 1982
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:http://www.timvw.be/>
Aug 18 '07 #2
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.
<snip>

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***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: porneL | last post by:
How do I use static functions/properties with inheritance? I've found it very problematic. class Foo { static function A() {/* ??? */::B(); } static function B() {echo 'Foo';} }; class...
3
by: wwight | last post by:
As many people have noticed by now, PHP exhibits some frustrating behavior when it comes to static fields and methods. For instance, when a static method is defined in a parent class, but called...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
64
by: groups | last post by:
C# is an impressive language...but it seems to have one big limitation that, from a C++ background, seems unacceptable. Here's the problem: I have a third-party Document class. (This means I...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.