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

Specializing Generic Type (or Method)

What is the current thinking about specializing generic C# types?

I've come across a situation where I'd like to do something like this:

struct X<T,U>
{
...
}

struct XCommonCase<T= X<T,bool>

Even if this example used classes and not structs, inheritance wouldn't
quite be the right thing here. The only way I know of to get this
specialization is to clone X in XCommonCase. It'd be nice if there
were something better.

It might also be useful to have similar specialization with generic
methods.

Jan 5 '07 #1
3 1617
<ab**@onezero.orga écrit dans le message de news:
11*********************@51g2000cwl.googlegroups.co m...

| What is the current thinking about specializing generic C# types?
|
| I've come across a situation where I'd like to do something like this:
|
| struct X<T,U>
| {
| ...
| }
|
| struct XCommonCase<T= X<T,bool>
|
| Even if this example used classes and not structs, inheritance wouldn't
| quite be the right thing here. The only way I know of to get this
| specialization is to clone X in XCommonCase. It'd be nice if there
| were something better.
|
| It might also be useful to have similar specialization with generic
| methods.

What is wrong with doing what you suggest ? As long as you use a class
instead of a struct and get the syntax right, this is perfectly permissible.

class X<T, U>
{
...
}

class XCommonCase<T: X<T, bool>
{
...
}

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 5 '07 #2
Joanna,

First of all, I am using a struct. In my application it is not
appropriate to use a class here.

Second, I do not think that inheritance is not the right relation
between the specialized type and the more general type. I cannot
explain exactly why. But one reason is that it does not make sense to
me that a class inheritance hierarchy would have to be introduced
simply in order to have a shorthand notation for the specialized
generic class XCommonCase. Generic specialization seems to me to be a
different kind of specialization than class specialization. Another
piece of evidence for this is that generic specialization makes sense
with structs.

Jan 5 '07 #3
Joanna Carter [TeamB] <jo****@not.for.spamwrote:

<snip>
What is wrong with doing what you suggest ? As long as you use a class
instead of a struct and get the syntax right, this is perfectly permissible.

class X<T, U>
{
...
}

class XCommonCase<T: X<T, bool>
{
...
}
The trouble is you then can't use X<T, booland XCommonCase<T>
interchangably, because an X<T, boolisn't necessarily an
XCommonCase<T>. Personally I really dislike using inheritance as a form
of typedef, which is what the above is, effectively. I have a prejudice
against inheritance in general though :)

--
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
Jan 5 '07 #4

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

Similar topics

4
by: Cool Guy | last post by:
I don't understand the third paragraph under the heading 'Generic type instantiations' on <http://msdn.microsoft.com/vcsharp/2005/overview/language/generics/>: | The .NET Common Language Runtime...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
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...
3
by: webcliff | last post by:
Please help me to look at the following code: -------------start---------- using System; using System.Collections.Generic; public class GenType { }
9
by: Andrus | last post by:
I need to create instance of generic type when generic type does not have parameterless constructor. I tried code below but got error shown in comment. How to fix ? Andrus. abstract class...
14
by: Ron | last post by:
I am trying to build a generic static method that will return a specified type from a method. My problem is that I need to return say an image from the method...and I'm having trouble...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.