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

C# Generics Problem: MSFT guys may know this

Hello All
I found some *issue* with the generics in C#. It looks like an issue to
me :

In C++ the following code compiles fine

class Person
{
public:
char last[10];
};
template <typename T>
class MyClass
{
T x;
public:
void call()
{
char copy[10];
strcpy(copy,x.last);
....
}
};

However a similar code fails to compile in C#
class Person
{
public string lastName;
}
class MyClass<T>
{
private T x;
public void Call()
{
string s = x.lastName;//COMPILER ERROR HERE
}
}

from what I understand, the compiler is not expanding the code ,rather
the code is *expanded* at runtime.
Does it mean you can never access any "function" of T even if you want
to do something like what I did above.
If this is true then it looks like a serious drawback of generics to
me.
because the compiler does a check for T t
}
}

Nov 17 '05 #1
4 1217
You must constrain the type paramter T (use "where" for this).

class Person
{
public string lastName;
}

class MyClass<T> where T : Person
{
private T x;
public void Call()
{
string s = x.lastName;
}
}
<co***********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello All
I found some *issue* with the generics in C#. It looks like an issue to
me :

In C++ the following code compiles fine

class Person
{
public:
char last[10];
};
template <typename T>
class MyClass
{
T x;
public:
void call()
{
char copy[10];
strcpy(copy,x.last);
...
}
};

However a similar code fails to compile in C#
class Person
{
public string lastName;
}
class MyClass<T>
{
private T x;
public void Call()
{
string s = x.lastName;//COMPILER ERROR HERE
}
}

from what I understand, the compiler is not expanding the code ,rather
the code is *expanded* at runtime.
Does it mean you can never access any "function" of T even if you want
to do something like what I did above.
If this is true then it looks like a serious drawback of generics to
me.
because the compiler does a check for T t
}
}

Nov 17 '05 #2

<co***********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello All
I found some *issue* with the generics in C#. It looks like an issue to
me :

In C++ the following code compiles fine

class Person
{
public:
char last[10];
};
template <typename T>
class MyClass
{
T x;
public:
void call()
{
char copy[10];
strcpy(copy,x.last);
...
}
};

However a similar code fails to compile in C#
class Person
{
public string lastName;
}
class MyClass<T>
{
private T x;
public void Call()
{
string s = x.lastName;//COMPILER ERROR HERE
}
}

from what I understand, the compiler is not expanding the code ,rather
the code is *expanded* at runtime.
That's exactly right. C++ templates are compiler magic. CLR Generics are
runtime magic. Notice they are CLR Generics, not C# Generics. They are
implemented in the .NET runtime, not the various language compilers.
Does it mean you can never access any "function" of T even if you want
to do something like what I did above.


You absolutely can. You have a couple of options.

Define an interface or base type which has the function and restrict the
generic to the interface.
Like this

interface ISurnamed
{
string LastName
{
get;
}
}
class MyClass<T>
where T : ISurnamed
{
T t = default(T);
void Foo()
{
string s = t.LastName;
}
}

Or, in a pinch, use you can always use reflection or emit a dynamic method
http://codeproject.com/csharp/GenericOperators.asp.

David
Nov 17 '05 #3
Don't create an interface. Just restrict the T in MyClass to Person.

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:Ol**************@tk2msftngp13.phx.gbl...
Define an interface or base type which has the function and restrict the
generic to the interface.
Like this

interface ISurnamed
{
string LastName
{
get;
}
}
class MyClass<T>
where T : ISurnamed
{
T t = default(T);
void Foo()
{
string s = t.LastName;
}
}

Or, in a pinch, use you can always use reflection or emit a dynamic method
http://codeproject.com/csharp/GenericOperators.asp.

David

Nov 17 '05 #4
Thanks Ted and dave

Ted Miller wrote:
Don't create an interface. Just restrict the T in MyClass to Person.

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:Ol**************@tk2msftngp13.phx.gbl...
Define an interface or base type which has the function and restrict the
generic to the interface.
Like this

interface ISurnamed
{
string LastName
{
get;
}
}
class MyClass<T>
where T : ISurnamed
{
T t = default(T);
void Foo()
{
string s = t.LastName;
}
}

Or, in a pinch, use you can always use reflection or emit a dynamic method
http://codeproject.com/csharp/GenericOperators.asp.

David


Nov 17 '05 #5

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

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
8
by: Leicester B. Ford Jr. | last post by:
I have come across a problem using generics. I want to create a Factory type that will build classes for me. Code snippet below... static public class Product<T> where T : new() { static...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
6
by: CassioT | last post by:
Hi. I want to create a base form class with a generic parameter. public BaseForm<T: Form {} public MyForm : BaseForm<MyType> {} The problem here is that the inherited form doesn't work in...
6
by: ffa | last post by:
In a class called Client I have an event called RefreshData (Public Event Refresh) which is fired whenever the Client class does a data fetch. In the main form I declare: Private WithEvents...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.