473,468 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generics a few questions

Hey
I have function like this:
GivenType GetData<GivenType>(...)
{
// I need to check if GivenType is System.String
the only way I found to work was:
if(typeof(GivenType) == Type.GetType("System.String")
// but here I want to do sth like this :
return (GivenType) String.Empty;

}

When I try to cast on GivenType which is in this case for 100% string I got
error in compile time, that casting is impossible. But if put String.Empty
into object type and then cast it will work fine. But it seems like not so
efficient so maybe there is a better way ? How to check for type on
GivenType any other ways than typeof ?
Jarod

May 29 '06 #1
3 1077
Jarod <bl*****@NOSPAM.gazeta.pl> wrote:
Hey
I have function like this:
GivenType GetData<GivenType>(...)
{
// I need to check if GivenType is System.String
the only way I found to work was:
if(typeof(GivenType) == Type.GetType("System.String")
// but here I want to do sth like this :
return (GivenType) String.Empty;

}

When I try to cast on GivenType which is in this case for 100% string I got
error in compile time, that casting is impossible. But if put String.Empty
into object type and then cast it will work fine. But it seems like not so
efficient so maybe there is a better way ? How to check for type on
GivenType any other ways than typeof ?


Well, aside from the fact that I'd use typeof(string) instead of
Type.GetType("System.String") I don't think the above is too bad, with
the extra cast to object just to make it compile:

return (GivenType) (object) string.Empty;

--
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
May 29 '06 #2
"Jarod" <bl*****@NOSPAM.gazeta.pl> wrote:
Hey
I have function like this:
GivenType GetData<GivenType>(...)
{
// I need to check if GivenType is System.String
the only way I found to work was:
if(typeof(GivenType) == Type.GetType("System.String")
I would write this "typeof(GivenType) == typeof(string)", but it is
still quite dodgy code.
// but here I want to do sth like this :
return (GivenType) String.Empty;
A string is castable only to a string or an object. If GivenType was an
int, would you expect the following to work:

return (int) String.Empty;

? How could it?
}
What are you really trying to do?
But if put String.Empty
into object type and then cast it will work fine.
But then it will *always* throw an exception at run time when GivenType
is neither string nor object.
But it seems like not so
efficient so maybe there is a better way ? How to check for type on
GivenType any other ways than typeof ?


What are you actually trying to do in this method?

-- Barry

--
http://barrkel.blogspot.com/
May 29 '06 #3
Barry Kelly <ba***********@gmail.com> wrote:

<snip>
But then it will *always* throw an exception at run time when GivenType
is neither string nor object.


No it won't - the whole point of the "if" block is to make sure it's
only special-casing a string.

Admittedly the method was missing a return value in other cases, but
something like this will work:

static T GetData<T>()
{
if (typeof(T)==typeof(string))
{
return (T) (object) string.Empty;
}
return default(T);
}
--
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
May 29 '06 #4

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...
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...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
10
by: Frank Rizzo | last post by:
Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I am converting them to List<and Dictionary<respectively. It's a pretty massive system, so there are a lot of casts. For...
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
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,...
1
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
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.