473,769 Members | 5,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics limitation on .Net

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 vs C++ templates.
Does anyone knows a workaround to do this ? Thx :

public class C<T>
{
private T myValue;

private void InitializeValue (out Byte value) { value = Byte.MinValue; }
private void InitializeValue (out Char value) { value = Char.MinValue; }
private void InitializeValue (out Int16 value) { value = Int16.MinValue; }
// ... and so on...
private void InitializeValue (out String value) { value = "Initial value"; }
private void InitializeValue (out Object value) { value = this; }

public C()
{
InitializeValue (out myValue);
}

// ...
}
Dec 28 '05
23 2552
Joanna,

I felt angry because of such remarks :

- "Tell me, what are you doing that requires such super-optimised code ? Most
business applications are only really as fast as the person typing at the
keyboard"
==> My question was 'I need speed', and your answer is "Why, nobody cares
about speed ?"

- "If speed is that much of an issue, then don't write code that invokes so
much boxing"
==> That was the purpose of my question : how to avoid so much boxing ?

- "The example that you give doesn't really seem to fit the generic
programming model"
==> You first denigrate my work and experience based on an silly example
without knowing the work I'm doing and my needs

Now, I apologize if my provious message hurts you, and I sincerly thanks you
for the time you gave me answering my messages (I really think that).

Regards
Luc

"Joanna Carter [TeamB]" wrote:
"Luc Vaillant" <Lu*********@di scussions.micro soft.com> a écrit dans le
message de news: 81************* *************** **...icrosof t.com...

| Ok, I'm bored feeding the troll...

I'm sorry you feel that way. I have never "trolled" newsgroups; had I done
so, I doubt that I would have been appointed as a newsgroup mediator for
Borland. You obviously have a problem with people who don't give you advice
or don't have opinions with which you agree.

| With your "years of experience in OO", you should felt that answer :

Hey, I am officially on holiday for Christmas. Just because I don't come up
with the right answer first time, doesn't give you the right to denegrate my
experience. If I don't understand your question fully the first ime around,
it may be that I am working on more than just solving your problems; like
writing articles for people that do appreciate the time and effort that goes
into trying to help others understand some pretty complicated stuff.

| Have you ever written or used generics ? Is Generics just a search/replace
| tool for you ?
|
| "In addition to type safety, generic collection types generally perform
| better for storing and manipulating value types because there is no need
to
| box the value types." taken from
|
"ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio .v80.en/dv_fxfund/html/6b90f9c3-eee3-4cd1-98d6-82019c83a1df.ht m"
:

It seems it takes a troll to think they have found an other troll :-) I find
your attitude to be insulting.

So, does the IComparer solve your problem ?

Have a peaceful New Year.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 30 '05 #21
"Luc Vaillant" <Lu*********@di scussions.micro soft.com> a écrit dans le
message de news: C2************* *************** **...icrosof t.com...

| Now, I apologize if my provious message hurts you, and I sincerly thanks
you
| for the time you gave me answering my messages (I really think that).

De rien :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 30 '05 #22
In the context of the OPs question: I would stay with the cast over convert,
as Convert.ChangeT ype "requires" the class implement IConvertible, or at the
very least Convert.ChangeT ype needs to know the conversion.

The cast doesn't care what the value implements...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Joanna Carter [TeamB]" <jo****@not.for .spam> wrote in message
news:en******** ******@TK2MSFTN GP11.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> a écrit
dans
| le message de news: OQ************* **@TK2MSFTNGP15 .phx.gbl...
|
|| You need to cast to object first, as Char to T is not defined. Without a
|| constraint only object to/from T is defined.
||
||| valueType = (T)(object)Char .MinValue;
|
| You can also use the Convert class :
|
| T value = (T) Convert.ChangeT ype(Char.MinVal ue, typeof(T))
|
| Joanna
|
| --
| Joanna Carter [TeamB]
| Consultant Software Engineer
|
|
Jan 3 '06 #23
| 1) How do I initialize a T value without doing boxing/unboxing ?

| Not possible, must use T value = (T)(Object)cons tant value
| even if I know that the constant value if of type T
Boxes the value! Remember (object) says to box value types!

You have two options on initializing T values without boxing:

1. default(T)
2. Parameterized constructor

NOTE: The reflection trick I showed earlier will box values.

| 2) How do I compare two T values without boxing/unboxing ?
Constrain T by IComparable<T>

http://msdn2.microsoft.com/en-us/library/4d7sx9hd.aspx

NOTE: System.ICompara ble<T> is different then System.ICompara ble!

In the case of simple equality, I would recommend constraining T by
IEquatable<T>.

http://msdn2.microsoft.com/en-us/lib...US,VS.80).aspx

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Luc Vaillant" <Lu*********@di scussions.micro soft.com> wrote in message
news:33******** *************** ***********@mic rosoft.com...
| Is that an answer ?
|
| I'm not writing a keyboard typing application, and I DO CARE ABOUT SPEED.
|
| > If speed is that much of an issue, then don't write code that invokes so
| > much boxing
|
| That's why I wanted to use Generics...
|
| > The example that you give doesn't really seem to fit the
| > generic programming model due to the possible differences in behaviour
| > required by the different types that may be used
|
| The example I gave was just to demonstrate the initialization issue. It
was
| not a real use case...
| Same remark about the 'Generics limitation' post I sent today. The example
| is to desmonstrate the != operator issue with generics, and this is not my
| real program...
|
| Why don't you try to answer my questions instead of explaining me that
most
| programs don't need speed, and that my examples don't "really seem to fit
the
| generic programming model".
|
| My questions are simple:
|
| 1) How do I initialize a T value without doing boxing/unboxing ?
|
| The answer is:
| Not possible, must use T value = (T)(Object)cons tant value
| even if I know that the constant value if of type T
|
| 2) How do I compare two T values without boxing/unboxing ?
|
| The answer is:
| Not possible from the answers I got in this post
|
|
| Regards
| Luc
|
|
| "Joanna Carter [TeamB]" wrote:
|
| > "Luc Vaillant" <Lu*********@di scussions.micro soft.com> a écrit dans le
| > message de news: 7F************* *************** **...icrosof t.com...
| >
| > | This will also box Char.MinValue into Object
| >
| > I won't deny that :-)
| >
| > Tell me, what are you doing that requires such super-optimised code ?
Most
| > business applications are only really as fast as the person typing at
the
| > keyboard.
| >
| > If speed is that much of an issue, then don't write code that invokes so
| > much boxing. The example that you give doesn't really seem to fit the
| > generic programming model due to the possible differences in behaviour
| > required by the different types that may be used. In which case, I would
| > tend to look elsewhere for a solution; generics are only really useful
when
| > the behaviour for *all* anticipated types is *identical*.
| >
| > Joanna
| >
| > --
| > Joanna Carter [TeamB]
| > Consultant Software Engineer
| >
| >
| >
Jan 3 '06 #24

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

Similar topics

2
5804
by: Hung Jung Lu | last post by:
Hi, I just downloaded rotor and gyro and am playing a bit with generics. I enclose a sample program. My question is, according to the language spec (http://msdn.microsoft.com/vcsharp/team/language/default.aspx, C# 2.0 Specification), a syntax like: public class MathEngine<T> where T: IHasX, IHasY { … }
8
1843
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 public T New() { return new T(); } }
4
9206
by: Michael Sparks | last post by:
I started writing some code with the 2.0 beta compiler, and found out quickly that specialization is not supported with generics. For example, I want to do something like this: class number ... class integer : number ... class real : number ... class multiplier<_number> where _number : number ... class multiplier<_number> where _number : integer ...
3
1252
by: Tom Jastrzebski | last post by:
Hello everybody, Here is another problem with generics I come across. Let's say I want to implement a structure performing some operations on some numeric type. I can not than just return this expected type since the compiler is not sure what this type would be, and it does not know that the type I am trying to returned is the correct type, or either that the conversion is possible. Ok, I understand - it is just the limitation of...
3
1957
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class Node<T> where T:IComparable<T> I don't like the syntax, and would prefer something that groups the constraint along with the type that it governs them, as depicted in this suggestion:
13
1697
by: Luc Vaillant | last post by:
I try to compare to values of generic value type T in a generic class as follow: public class C<T> where T : struct { private T value1; private T value2; C(T value1, T value2) {
8
2433
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public MyClass() { } public void AppendItem()
8
2554
by: Gary Brown | last post by:
Hi, When I do the following public class IAmAGeneric<T> { ... public T AMember (T a, T b) { return a + b; }; }
2
138
by: =?Utf-8?B?QnJhdmVzQ2hhcm0=?= | last post by:
I am trying to convert a class I have to generics and I can't seem to find any possible why to implement it. I'm beginning to think I'm doing something I shouldn't or I hit generics limitation. Here is an example of the old way: static void Main(string args) { MonitorCenter monitor = new MonitorCenter(); monitor.SetMonitor(new ExceptionMonitor());
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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 we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.