A Generic Generics Problem

R

rkausch

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 application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two languages. I would like to use a class that
is defined with Generics, only without having to declare its type
everywhere.

For example, suppose I'm using an IList, and in one location, I
declare it using a generic type of <String>. Now, in a different
class, I'd like to store that list as a field, but I don't want to
store the type associated with it. If I'm required to store the type
with the field (such as <pre>private IList<String> fieldName;</pre>,
then the class in which it is stored will be required to be generic,
and any classes that store instances of it will also need to be
generic, ad infinitum, until all my code is generified all over the
place (which I don't want!). Now, the example presented here is
easily solvable, since the IList interface is declared twice (once in
System.Collections as a non-generic version, and once in
System.Collections.Generic as a generic version).

Here's a code example of the Java equivalent, followed by the
(illegal) desired C# syntax:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}

public class ExampleImpl<Type> implements ExampleInterface<Type>
{
// method implementations here
}

public class SampleClass
{
private ExampleInterface mExampleInterface;

public void setExampleInterface(ExampleInterface
pExampleInterface)
{
mExampleInterface = pExampleInterface;
}
}

public class SampleBuilder
{
private SampleClass mSampleClass;

public SampleBuilder()
{
mSampleClass = new SampleClass();
mSampleClass.setExampleInterface(new ExampleImpl<String>());
}
}

// end Java example
</pre>

Desired C# implementation:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}

public class ExampleImpl<Type> : ExampleInterface<Type>
{
// method implementations here
}

public class SampleClass
{
private ExampleInterface exampleInterface;

public void SetExampleInterface(ExampleInterface exampleInterface)
{
this.exampleInterface = exampleInterface;
}
}

public class SampleBuilder
{
private SampleClass sampleClass;

public SampleBuilder()
{
this.sampleClass = new SampleClass();
this.sampleClass.SetExampleInterface(new
ExampleImpl<String>());
}
}

// end C# example
</pre>

So, in the C# example, if I'm forced to store the instance of
<pre>ExampleInterface</pre> with its generic type, then
<pre>SampleClass</pre> will have to be defined as a Generic class, and
stored in SampleBuilder with its generic type, which will then force
<pre>SampleClass</pre> to also be defined as a Generic class, and so
on and so forth.

Is there any way to do what I'd like, or am I forced to abandon the
use of Generics completely in C#?

Thanks in advance!
Respectfully,
Robert Kausch
 
L

Ludwig

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 application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two languages. I would like to use a class that
is defined with Generics, only without having to declare its type
everywhere.

For example, suppose I'm using an IList, and in one location, I
declare it using a generic type of <String>. Now, in a different
class, I'd like to store that list as a field, but I don't want to
store the type associated with it. If I'm required to store the type
with the field (such as <pre>private IList<String> fieldName;</pre>,
then the class in which it is stored will be required to be generic,
and any classes that store instances of it will also need to be
generic, ad infinitum, until all my code is generified all over the
place (which I don't want!). Now, the example presented here is
easily solvable, since the IList interface is declared twice (once in
System.Collections as a non-generic version, and once in
System.Collections.Generic as a generic version).

Here's a code example of the Java equivalent, followed by the
(illegal) desired C# syntax:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}

public class ExampleImpl<Type> implements ExampleInterface<Type>
{
// method implementations here
}

public class SampleClass
{
private ExampleInterface mExampleInterface;

public void setExampleInterface(ExampleInterface
pExampleInterface)
{
mExampleInterface = pExampleInterface;
}
}

public class SampleBuilder
{
private SampleClass mSampleClass;

public SampleBuilder()
{
mSampleClass = new SampleClass();
mSampleClass.setExampleInterface(new ExampleImpl<String>());
}
}

// end Java example
</pre>

Desired C# implementation:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}

public class ExampleImpl<Type> : ExampleInterface<Type>
{
// method implementations here
}

public class SampleClass
{
private ExampleInterface exampleInterface;

public void SetExampleInterface(ExampleInterface exampleInterface)
{
this.exampleInterface = exampleInterface;
}
}

public class SampleBuilder
{
private SampleClass sampleClass;

public SampleBuilder()
{
this.sampleClass = new SampleClass();
this.sampleClass.SetExampleInterface(new
ExampleImpl<String>());
}
}

// end C# example
</pre>

So, in the C# example, if I'm forced to store the instance of
<pre>ExampleInterface</pre> with its generic type, then
<pre>SampleClass</pre> will have to be defined as a Generic class, and
stored in SampleBuilder with its generic type, which will then force
<pre>SampleClass</pre> to also be defined as a Generic class, and so
on and so forth.

Is there any way to do what I'd like, or am I forced to abandon the
use of Generics completely in C#?

Thanks in advance!
Respectfully,
Robert Kausch

Maybe:


public interface ExampleInterfaceNG {}

public interface ExampleInterface<T> : ExampleInterfaceNG {}

public class SampleClass
{
private ExampleInterfaceNG exampleInterface;

public void SetExampleInterface(ExampleInterfaceNG
exampleInterface)
{
this.exampleInterface = exampleInterface;
}
}
 
J

Jon Skeet [C# MVP]

[Removed microsoft.public.dotnet.csharp.general which isn't a valid
newsgroup]


Is there any way to do what I'd like, or am I forced to abandon the
use of Generics completely in C#?

Well, you've effectively abandoned the use of generics in Java already,
by using the raw type instead of the generic type. Java generics are
relatively weak, only working at compile-time using type erasure. C#
and .NET generics are stronger, maintaining the type information at
execution time too.

Now, what you're trying to do is effectively abandon the type safety
that generics provides by saying

private ExampleInterface exampleInterface;

What information is that meant to convey? How would you use it? You no
longer have any idea what types any of the methods take.

So basically, the equivalent to the Java code is to abandon generics.
I'd stick to real generics in the C# code - it will only "infect" you
with generics as far as you want the types that you're talking about to
be generic. Once you get to the stage that you know which real type you
want to use, you can specify that and no longer make the type itself
generic. Basically, if you want the benefits of generics, you've got to
be able to provide the user of a class with a way of specifying the
type arguments to use for those benefits, which you can't do if you
don't have type parameters.
 
R

rkausch

Well, you've effectively abandoned the use of generics in Java already,
by using the raw type instead of the generic type. Java generics are
relatively weak, only working at compile-time using type erasure. C#
and .NET generics are stronger, maintaining the type information at
execution time too.

Now, what you're trying to do is effectively abandon the type safety
that generics provides by saying

private ExampleInterface exampleInterface;

What information is that meant to convey? How would you use it? You no
longer have any idea what types any of the methods take.

So basically, the equivalent to the Java code is to abandon generics.
I'd stick to real generics in the C# code - it will only "infect" you
with generics as far as you want the types that you're talking about to
be generic. Once you get to the stage that you know which real type you
want to use, you can specify that and no longer make the type itself
generic. Basically, if you want the benefits of generics, you've got to
be able to provide the user of a class with a way of specifying the
type arguments to use for those benefits, which you can't do if you
don't have type parameters.

Hi Jon-

Thanks for the quick reply. If I recall correctly, since Java uses
generics in such a way as to not break the legacy JVMs, it basically
replaces all instances of "T" with the actual type at compile time,
and effectively retains the type safety at run time (you'll get a
class cast exception if you try to put something other than type T
into the object, for example). I realize that C# generics are not
implemented this way, which is the crux of the situation. Basically,
I suppose I'm asking for the impossible: the type safety of generics
to be preserved, regardless of how the pointer to the object is being
used (this probably would work in C++ using templates, since n classes
are generated, one for each variation of type T used). Does the
underlying object preserve the type information after it's created, or
is that always handled by the syntax of the declaration (e.g.
List<String> always knows that it can only take a String because the
syntax says <String>, not because the List object preserved String
internally)?

Thanks again!
Robert
 
R

rkausch

Maybe:

public interface ExampleInterfaceNG {}

public interface ExampleInterface<T> : ExampleInterfaceNG {}

public class SampleClass
{
private ExampleInterfaceNG exampleInterface;

public void SetExampleInterface(ExampleInterfaceNG
exampleInterface)
{
this.exampleInterface = exampleInterface;
}

}

Ludwig-

I've explored this a bit, and ran into some problems with how I'm
using the objects..

basically:
public interface Sample
{
void save(Object object);
}

public interface Sample<Type>
{
void save(Type object);
}

public class Example
{
private Sample mSample;

public Example()
{
mSample = new Sample<String>(); // I realize you can't
instantiate an interface
// pseudo-
code to prevent more typing
}
}
 
R

rkausch

<snip>

Perhaps a good followup question to this issue is: Do C# Generics
support wildcards?

For example, in Java, I can say
private List<?> foo;

which will ensure the type safety of the underlying list, I believe.
There's other syntactic sugar that derives from this:
private List<? extends Number> foo;
private List<? implements Closable> bar;
// etc...

I saw the deal about limiting Generics when declaring the type,
basically:
public class Foo<T> : where T extends Number
{
// etc,

can you do this on an individual field?

Thanks again!
Robert
 
J

Jon Skeet [C# MVP]

Thanks for the quick reply. If I recall correctly, since Java uses
generics in such a way as to not break the legacy JVMs, it basically
replaces all instances of "T" with the actual type at compile time,
and effectively retains the type safety at run time (you'll get a
class cast exception if you try to put something other than type T
into the object, for example).

No - it replaces all instances of "T" with java.lang.Object in the
generic type, and puts runtime casts in the calling code. Here's an
example:

import java.io.*;
import java.util.*;

public class Test
{
public static void main (String[] args)
{
ArrayList<String> strings = new ArrayList<String>();
strings.add("Hello");

Object foo = strings;

ArrayList<File> files = (ArrayList<File>) foo;

System.out.println ("Got past the cast");

File x = files.get(0);
}
}

You'll get a warning at compile time, but it *will* compile. It will
then fail later than you really want.
I realize that C# generics are not
implemented this way, which is the crux of the situation. Basically,
I suppose I'm asking for the impossible: the type safety of generics
to be preserved, regardless of how the pointer to the object is being
used (this probably would work in C++ using templates, since n classes
are generated, one for each variation of type T used).

But my point is that the object couldn't be particularly usefully used
any more, because you no longer (at compile-time) have any information
about the original type arguments.

Now, you could make your type implement a non-generic interface if you
don't need to expose the type parameters in the API, but if you want to
keep a strongly typed API, you need the type parameters.
Does the
underlying object preserve the type information after it's created, or
is that always handled by the syntax of the declaration (e.g.
List<String> always knows that it can only take a String because the
syntax says <String>, not because the List object preserved String
internally)?

No, a List<string> preserves its type. If you try the equivalent to the
above code with C#, you'll get a casting exception when you cast the
list, not when you try to access elements within it.
 
J

Jon Skeet [C# MVP]

<snip>

Perhaps a good followup question to this issue is: Do C# Generics
support wildcards?

For example, in Java, I can say
private List<?> foo;

No - that, along with covariance/contravariance, are the things Java
generics *do* have over .NET generics. Other than that, .NET generics
rock very hard compared with Java generics.
 
R

rkausch

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 application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two languages. I would like to use a class that
is defined with Generics, only without having to declare its type
everywhere.

For example, suppose I'm using an IList, and in one location, I
declare it using a generic type of <String>. Now, in a different
class, I'd like to store that list as a field, but I don't want to
store the type associated with it. If I'm required to store the type
with the field (such as <pre>private IList<String> fieldName;</pre>,
then the class in which it is stored will be required to be generic,
and any classes that store instances of it will also need to be
generic, ad infinitum, until all my code is generified all over the
place (which I don't want!). Now, the example presented here is
easily solvable, since the IList interface is declared twice (once in
System.Collections as a non-generic version, and once in
System.Collections.Generic as a generic version).

Here's a code example of the Java equivalent, followed by the
(illegal) desired C# syntax:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here

}

public class ExampleImpl<Type> implements ExampleInterface<Type>
{
// method implementations here

}

public class SampleClass
{
private ExampleInterface mExampleInterface;

public void setExampleInterface(ExampleInterface
pExampleInterface)
{
mExampleInterface = pExampleInterface;
}

}

public class SampleBuilder
{
private SampleClass mSampleClass;

public SampleBuilder()
{
mSampleClass = new SampleClass();
mSampleClass.setExampleInterface(new ExampleImpl<String>());
}

}

// end Java example
</pre>

Desired C# implementation:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here

}

public class ExampleImpl<Type> : ExampleInterface<Type>
{
// method implementations here

}

public class SampleClass
{
private ExampleInterface exampleInterface;

public void SetExampleInterface(ExampleInterface exampleInterface)
{
this.exampleInterface = exampleInterface;
}

}

public class SampleBuilder
{
private SampleClass sampleClass;

public SampleBuilder()
{
this.sampleClass = new SampleClass();
this.sampleClass.SetExampleInterface(new
ExampleImpl<String>());
}

}

// end C# example
</pre>

So, in the C# example, if I'm forced to store the instance of
<pre>ExampleInterface</pre> with its generic type, then
<pre>SampleClass</pre> will have to be defined as a Generic class, and
stored in SampleBuilder with its generic type, which will then force
<pre>SampleClass</pre> to also be defined as a Generic class, and so
on and so forth.

Is there any way to do what I'd like, or am I forced to abandon the
use of Generics completely in C#?

So, here's another example that might shed some light here... I
unfortunately can't get too specific (proprietary company reasons ;)

basically, imagine you have a database accessor thingy, and you want
that thingy to access rows and columns.
nope
You have written a class to access individual columns, as an object
(call it Column.cs)
Column.cs contains an accessor, and that accessor is Generic in Java
(though stored without being generic), and all data is read from that
accessor
though, in C#, the language forces you into storing the accessor with
its type.
Which means to get the type, you now need to declare Column as a
generic class (Column<Type>)
now, since row must have all of those columns stored in it, it must
now use generics as well
because you can't do List<Column> as a storage object
you'd have to do List<Column<String>>, List<Column<Int32>>, etc, one
for each different datatype you have.
that's a good analogy, I'm going to post that, see if it gets any more
responses...
Jason: good analogy, but I don't think thats right
as far as doing it in C#
Jason is typing...

Basically, imagine you have a database accessor class, and you want
that class to access rows and columns.
You have written a class to access individual columns, as an object
(call it Column.cs)

Column.cs contains an accessor, and that accessor is Generic in Java
(though stored without being generic), and all data is read from that
accessor. Though, in C#, the language forces you into storing the
accessor with its type.

Which means to get the type, you now need to declare Column as a
generic class (Column<Type>).

Now, since row must have all of those columns stored in it, it must
now use generics as well, because you can't do List<Column> as a
storage object, you'd have to do List<Column<String>>,
List<Column<Int32>>, etc, one for each different datatype you have.

Thanks again!
Robert
 
R

rkausch

<snip>
A further refined example of the type of problem I'm dealing with,
though I can't get too specific for company proprietary reasons ;):

Basically, imagine you have a database accessor Class, and you want
that Class to access rows and columns.

You have written a class to access individual columns, as an object
(call it Column.cs)

Column.cs contains an accessor, and that accessor is Generic in Java
(though stored without being generic), and all data is read from that
accessor

In C#, the language forces you into storing the accessor with its
type. Which means to get the type, you now need to declare Column as
a generic class (Column<Type>)

Now, since row must have all of those columns stored in it, it must
now use generics as well
because you can't do List<Column> as a storage object you'd have to do
List<Column<String>>, List<Column<Int32>>, etc, one for each different
datatype you have.

Thanks again!
Robert
 
J

Jon Skeet [C# MVP]

Column.cs contains an accessor, and that accessor is Generic in Java
(though stored without being generic), and all data is read from that
accessor. Though, in C#, the language forces you into storing the
accessor with its type.

Only if you want to use the generic type as the variable type. Here's
an example of not doing so:

IList list = new List<string>();

This is still type-safe - I can't add any non-string references to the
list - but the type safety is now only at execution time, not at
compile-time.
 
R

rkausch

Only if you want to use the generic type as the variable type. Here's
an example of not doing so:

IList list = new List<string>();

This is still type-safe - I can't add any non-string references to the
list - but the type safety is now only at execution time, not at
compile-time.

Jon-

Thanks for the reply, I think we might be getting closer to closing
the gap that I'm trying to bridge... Now, Ludwig earlier suggested
that we use a non-generic base class to do this. As I explained to
him earlier, I still need the methods defined in the interface
(whether they're defined in the Generic or Non-Generic sense) in the
Column class, and again, I'd prefer not to make Column generic.

So, here's basically the guts of what I'm trying to do:
public class Column
{
private ColumnAccessor accessor;

public Object GetValue()
{
return accessor.GetValue();
}
}

public interface IColumnAccessor<Type>
{
Type GetValue();
}

public class ColumnAccessor<Type> : IColumnAccessor<Type>
{
public Type GetValue()
{
// get the value here from elsewhere.
}
}

// now, if we follow Ludwig's example (and indeed, C#'s Collection
framework with IList & IList<T>),
// do we then define:
public IColumnAccessor
{
Object GetValue();
}

public IColumnAccessor<Type> : IColumnAccessor
{
Type GetValue();
}

Is there a huge flaw in logic that I'm missing here? Any suggestions
on an alternate way to tackle this?

Thanks again!
Robert
 
J

Jon Skeet [C# MVP]

Is there a huge flaw in logic that I'm missing here? Any suggestions
on an alternate way to tackle this?

You haven't implemented IColumnAccessor in ColumnAccessor<Type> (you
should avoid using real type names as type parameters, by the way).
Interfaces can't be implemented with covariant return types.

You need something like:

public class ColumnAccessor<T> : IColumnAccessor<T>
{
public T GetValue()
{
// get the value here from elsewhere.
}

object IColumnAccessor.GetValue()
{
return GetValue();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top