473,396 Members | 2,010 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.

generic with constraint of type int

I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs
and they showed that any value type can be used unless it is nullable.
Why doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.
dan
May 16 '06 #1
6 4619
It means exactly what it says. If you have class Metadata<T> where T: struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs and
they showed that any value type can be used unless it is nullable. Why
doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for more
information.
dan

May 16 '06 #2
Dan Holmes <da*******@bigfoot.com> wrote:
I have a class that i need a constraint of int, string, float or bool.
You cannot express such a constraint using generic constraints. You'll
need to put an imperative test in the constructor.

Do for example:

---8<---
class A<T>
{
public A()
{
if (!(T is Int32) || // ... etc.
}
}
--->8---
Why doesn't this work?

public class Metadata<T>
where T: System.int
A new type cannot descend from an existing value type, so this
constraint would restrict the generic type to exactly one possible
instantiation, Metadata<int>.
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.


This documentation is describing the literal constraint 'where T:
struct'. The 'struct' in this constraint description is not a
placeholder. The 'where T: struct' constraint constrains the type
parameter to be a value type other than nullable.

-- Barry
May 16 '06 #3
Barry Kelly <ba***********@gmail.com> wrote:
if (!(T is Int32) || // ... etc.


Correction:

if (typeof(T) != typeof(Int32) && // ... etc.

-- Barry
May 16 '06 #4
What is the best way to handle this? Check the type of T in the
constructor and throw if T isn't one of the valid types?

dan
Clive Dixon wrote:
It means exactly what it says. If you have class Metadata<T> where T: struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs and
they showed that any value type can be used unless it is nullable. Why
doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for more
information.
dan


May 16 '06 #5
That's the only way I can see of doing what you want, though it isn't ideal
since you're only catching at runtime and not at compile time.

--
======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2*****************@TK2MSFTNGP05.phx.gbl...
What is the best way to handle this? Check the type of T in the
constructor and throw if T isn't one of the valid types?

dan
Clive Dixon wrote:
It means exactly what it says. If you have class Metadata<T> where T:
struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs
and they showed that any value type can be used unless it is nullable.
Why doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.
dan


May 16 '06 #6
A better way to do it (although not ideal, because like the other it is
not caught at compile time) would be to check the type in the static
constructor, since that is done only once.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Clive Dixon" <cl*******************@digita.noluncheonmeat.com > wrote in
message news:el**************@TK2MSFTNGP02.phx.gbl...
That's the only way I can see of doing what you want, though it isn't
ideal since you're only catching at runtime and not at compile time.

--
======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2*****************@TK2MSFTNGP05.phx.gbl...
What is the best way to handle this? Check the type of T in the
constructor and throw if T isn't one of the valid types?

dan
Clive Dixon wrote:
It means exactly what it says. If you have class Metadata<T> where T:
struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed
here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool.
I have tried the following but can't make VS accept it. I read the
docs and they showed that any value type can be used unless it is
nullable. Why doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.
dan

May 16 '06 #7

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...
9
by: Alon Fliess | last post by:
Hi I am trying to write a generic class that instantiates the generic type, but I can not find the correct way to give it the constructor constraint. For example: In C#: class X<T> where...
4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
2
by: Brian Richards | last post by:
I'm trying to write a generic function (List<TPanelType> GetGenericPanels<TPanelType, TObjectType>()) that returns all UserControls that derive from T and and implement an interface...
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.
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: Bliss | last post by:
I have some problems with my generic. Part of code: using System; using System.Collections.Generic; using System.Text; namespace Gnrc { public class Gnrc<T>
2
by: puzzlecracker | last post by:
Unlike C++, in Csharp you're only allowed to compare a generic type T with null, if the method it's passed in not implementing IComparable<Tor , IEquatable<T(still don't know why we need these...
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...
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.