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

Generics HowTo

How to ensure that generic Collection
accept only String as a typeparam?
This causes an compiler error...

public class StringCollection<T> : List<T> where T : String

{

}
Nov 17 '05 #1
5 1087
Jacek Jurkowski <pc*****@priv1.onet.pl> wrote:
How to ensure that generic Collection
accept only String as a typeparam?
This causes an compiler error...

public class StringCollection<T> : List<T> where T : String
{

}


If you only want to be able to accept a single type as the type
parameter, it's not really generic any more. Just use:

public class StringCollection : List<string>
{
}

--
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 #2
Dou You think I better derive from CollectionBase instead
of using generics? Why a cant use "where T : String"?
If you only want to be able to accept a single type as the type
parameter, it's not really generic any more. Just use:

public class StringCollection : List<string>
{
}


Its still possible: StringCollection<int> = new StringCollection<int>();
Nov 17 '05 #3
If you read the error message it makes pretty clear that
"A constraint must be an interface, a non-sealed class or a type parameter".
String is a sealed class.

Ab.
http://joehacker.blogspot.com

"Jacek Jurkowski" <pc*****@priv1.onet.pl> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Dou You think I better derive from CollectionBase instead
of using generics? Why a cant use "where T : String"?
If you only want to be able to accept a single type as the type
parameter, it's not really generic any more. Just use:

public class StringCollection : List<string>
{
}


Its still possible: StringCollection<int> = new StringCollection<int>();

Nov 17 '05 #4

"Jacek Jurkowski" <pc*****@priv1.onet.pl> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Dou You think I better derive from CollectionBase instead
of using generics? Why a cant use "where T : String"?
If you only want to be able to accept a single type as the type
parameter, it's not really generic any more. Just use:

public class StringCollection : List<string>
{
}


Its still possible: StringCollection<int> = new StringCollection<int>();


It shouldn't be, since StringCollection has no generic type. You don't want

public class StringCollection<T> : List<string> { };

you want
public class StringCollection : List<string> { };

You might want to look into the Collection<T> class as well if you need more
flexibility than List<T> offers.
Nov 17 '05 #5
Thanks for Your help :-)
Nov 17 '05 #6

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

Similar topics

4
by: Josef Sachs | last post by:
Is Andrew Kuchling's regex-to-re HOWTO available anywhere? I've found the following (dead) links on various Web pages: http://py-howto.sourceforge.net/regex-to-re/regex-to-re.html...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
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...
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: 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: 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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.