473,387 Members | 1,497 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,387 software developers and data experts.

possible generic inheritance bug

Hi,

Either I am missing something, or there is a compiler bug in the way
inheritance from generic s is treated.

In the following code, class c2 inherits from c1_generic. c1_generic
has T as a generic paramter, and in the inheritance, class c2 specifies
c3_T as the value of this parameter.

class c1_generic defines 2 constructors, 1 of them taking 1 parameter.

Attempting to instatiate an object of class c2 using this constructor
fails unless c2 also redefines this constructor.

Here is the code to reproduce the problem:

// base generic class. defines 2 constructors, one of them taking 1
parameter
public class c1_generic<T> {
public c1_generic() {
}

public c1_generic(object o) {
}
}

// inherit from c1_generic, with c3_T as the generic class parameter
public class c2 : c1_generic<c3_T> {

// if you remove this constructor, it won't work - complains that
// it doesn't find a constructor taking 1 parameter, even though such
a
// constructor is defined in the parent class
public c2(object o) : base(o) {
}
}

public class c3_T {
}

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
// if you remove the constructor in c2, this
fails - it can't find
// the constructor defined in c1_generic:
c2 o = new c2(this);
}

Comments?

Thanks,

YanivG

Nov 17 '05 #1
2 1619
ya*********@gmail.com wrote:
Hi,

Either I am missing something, or there is a compiler bug in the way
inheritance from generic s is treated.

In the following code, class c2 inherits from c1_generic. c1_generic
has T as a generic paramter, and in the inheritance, class c2 specifies
c3_T as the value of this parameter.

class c1_generic defines 2 constructors, 1 of them taking 1 parameter.

Attempting to instatiate an object of class c2 using this constructor
fails unless c2 also redefines this constructor.


Constructors are never inherited, and that holds for non-generic types
as well.

Example:

public class c1
{
public c1()
{
}

public c1(Int32 o)
{
}
}

public class c2 : c1
{
// nothing here
}

static Main()
{
c2 c = new c2(10); // this won't compile
}

If your class has no constructor defined at all, a public one with no
parameters will be defined for you. This will in turn call the
constructor in the base class with no arguments as well, which is why I
defined public c1() in c1, otherwise c2 would not compile either.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #2
<ya*********@gmail.com> wrote:
Either I am missing something, or there is a compiler bug in the way
inheritance from generic s is treated.


No, you're missing the fact that constructors aren't inherited, as
Lasse said.

See http://www.pobox.com/~skeet/csharp/constructors.html for more
information on constructors.

--
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
Nov 17 '05 #3

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

Similar topics

2
by: Jasper Kent | last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to avoid long instantiation names. I can do this okay: using BigDictionary = System.Collections.Generic.Dictionary<int,...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
28
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
3
by: abiz | last post by:
What is the current thinking about specializing generic C# types? I've come across a situation where I'd like to do something like this: struct X<T,U> { ... } struct XCommonCase<T=...
2
by: Angel Mateos | last post by:
I have this structure: Class ElemBase Class Elem1 : Inherits ElemBase Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits System.ComponentModel.BindingList(Of GenElem) Class Colec1...
4
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.