generics

When not to implement a generic interface?

Is there a general rule for when you should and shouldn't have a generic interface? My case in point is a simple data frame interface. There is a "user data" member to allow the implementation to attach any implementation-specific data that needs to go with the frame. I don't know whether to leave it as an object type (and require the...

How to handle generic inheritance

I have this code for example public class BaseClass {} public class DerivedClass1 : BaseClass {} public class DerivedClass2 : BaseClass { } public class DerivedClass3 : BaseClass { } public class GenericBaseClass<T> where T : BaseClass { public List<T> collection { get; set; } } Now I'd like to create a new class which would i...

Descendant non-generic class into Base generic class

Usually I Use interfaces or base classes as paramter types when passing derived objects to a method. For example Method1(ICar car); Method2(BaseClass derivedClass); But what about a Generic base class when the descendant class isn't generic ? public class GenericBaseClass<T> where T : BaseClass {} public class GenericDerivedClass1 :...

How to call method of instance of generic type when it's type argument is unknown?

This seems like basic problem, but I'm struggling with it (maybe because of tiredness). E.g. - if i create instance of repository like this => var repositoryType = typeof(Repository<>).MakeGenericType(entityType); // repository type==object :( var repository = ServiceLocator.Current.GetInstance(repositoryType); What's the best way t...

.net 4 generics question

Hi, I have the following class structure: public class A : AInterface { } public interface AInterface { } public class B<T> : BInterface<T> where T : AInterface { public T Element { get; set; } } public interface BInterface<T> where T : AInterface { T Element { get; set; } } public class Y : B<A> { } public class Z<T> where...

binding generic list to dropdown

I have public class ListA { private string code; private string name; public string Code { get { return code; } set { code = value; } } public string Name { get { return name; } set { name = value; } } } List<ListA> lst = new List<ListA>(); public List<ListA> getList() { lst.Add(new ListA { Code = "ABC"...

Understanding Generics

@Entity @Table(name = "your_entity") public class YourEntityClass implements IEntity<Long> { @Id @SequenceGenerator(name = "gen_test_entity2_id", sequenceName = "test_entity2_id_seq", allocationSize = 10) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen_test_entity2_id") pr...

Rewrite type specific method as generic

I have a method (shown below) that I discovered can be reused in code elsewhere if I could turn it into a generic method, but am struggling with the syntax and could use a bit of help: Sample: private List<IndexEntry> AddParentReferences(List<IndexEntry> listWithoutParents) { List<IndexEntry> listWithParents = new L...

What does the following C# code do?

I ran across the following class in a C# XNA graphics api and I am not sure what it does or that it needs to be so obscure. (T is constrained to be a struct in a parent class) static class Ident { static object sync = new object(); static volatile int index = 0; static int Index { get ...

Should overloaded methods with value types be consolidated into a generic method ?

Hi, When performing OO design, is it better to consolidate a collection of methods that use simple value types into a generic method ? For example: public int Sum(int x, int y) // Overload with float. public float Sum(float x, float y) Consolidated to: public T Sum<T> (T x, T y) Thanks, Scott ...

One Base class for Pages, Master and UserControl

How to do like this: public class Base<T>: T{ public OneProp{ get{ return new OneProp(); } } } public class BasePage: Base<Page>{} public class BaseMaster: Base<MasterPage>{} public class BaseUserControl: Base<UserControl>{} Extension methods are not good, cause they are static and not property. Also we have copy-paste methodology...

Monad trait in Scala

(How) is it possible to represent monads in Scala in a generic way (like the Monad typeclass in Haskell)? Is it somehow possible to define a trait Monad for this purpose? ...

C# generics - without lower bounds by design?

I was reading an interview with Joshua Bloch in Coders at Work, where he lamented the introduction of generics in Java 5. He doesn't like the specific implementation largely because the variance support—Java's wildcards—makes it unnecessarily complex. As far as I know, C# 3 doesn't have anything like explicit, bounded wildcards, e.g. yo...

A problem with parametrized return value type

Hello, I am somewhat new to the Java Generics feature. Can you please help me to figure out why the code snippet below does not work: interface Response {} interface Request<T extends Response> {} class TestResponse implements Response {} class TestRequest implements Request<TestResponse> {} <T extends Response> T foo(Request<T> b) ...

C# WPF: Using Generic Views: Not Possible?

I am new to WPF, have read some books about it, but now I am stuck. I have a generic class and a few concrete implementation of it public class Question<T> { List<T> AvailableAnswers; T Correct Answer; MidiSound SoundFragment; String Question; } public class IntervalQuestion : Question<Interval> public class ScaleQuest...

Combining use of wildcard in methods and in type of receiver in Java.

Hi, I am trying to use a combination of wildcards in the type of the receiver and in the type of an argument to a method in Java. The context is that of defining a container. Now, the type Container should not admit insertions whatsoever, since this type does not specify the type of the contained objects. However, if the underlying data...

What is the correct file name for the interface "IRepository<T>"

Just curious what people would name the file that contains the generic interface IRepository<T>. IRepositoryT.cs? Update One minor point to add. Normally, when you change the name of a class file, Visual Studio asks if you want to do a rename on the class as well. In the case of IRepository<T>, no matter what I name the file (IReposit...

How to use generics with overloading?

Edit: C# 3.0, net 3.5. I am C++ programmer, so maybe I miss some simple solution in C#. Simplified example: I have several classes inherited from class MyBase (with method Inc). The inherited classes may override Inc. I have several overloaded "functions" (static methods) for the inherited classes: void Print(MyInherited1 i1) .... voi...

Method has the same erasure as another method in type

Why is it not legal to have those two methods in the same class? class Test{ void add(Set<Integer> ii){} void add(Set<String> ss){} } I get the complication error "Method add(Set) has the same erasure add(Set) as another method in type Test". while I can work around it, I was wondering why javac doesn't like this. I can see tha...

Conversion of IEnumerable<T> to IList

If a method takes a parameter of type System.Collections.IList can I legitimately/safely pass a value of type System.Collections.Generic.IEnumerable<T>? I would appreciate a thorough explanation of why this is possible and what actually happens to the object T when the IEnumerable<T> is used inside of the method. Is it converted to t...