generics

How do I make a property of custom control to be a generic type ?

I have created a custom ASP.NET control ( derived from WebControls.TextBox). I want to add a property to that control which will be of a type. This property will actually always be some type of enum . So when in the designer I look at the properties window of that control - I want to be able to assign value to that property by a selec...

c# compare two generic values

I've coded something like this: public bool IsDataChanged() { T value1 = GetValue2; T value2 = GetValue1(); return (valueInDB != valueFromView); } Right now the function doesn't compile with the error "Operator '!=' cannot be applied to operands of type 'T' and 'T'". What do I have to do to make this function w...

Java Generics

Can someone explained, as detailed as possible, the differences between the following types? List List<Object> List<?> Can I get an answer, not a link? Let me make this more specific. When would I want to use public void CanYouGiveMeAnAnswer( List l ){} ? When would I want to use public void CanYouGiveMeAnAnswer( List<Object...

Linq - Casting IQueryable to IList returns null - WHY?

I have to be missing something obvious here. I don't get why this cast of the results of a linq query returns null and not the typed list I'm requesting. IList<IMyDataInterface> list = query.ToList() as IList<IMyDataInterface>; The complete code to run this is below. This is a knowledge gap I need to bridge. I have tried all kinds of ...

Why it is not posible to define generic indexers in .NET?

Does anyone knows why you can't create a generic indexer in .NET the following code throws a compiler error: public T this<T>[string key] { get { /* Return generic type T. */ } } Does this mean you can't create a generic indexer for a generic member collection? ...

Add Generic list in a MyClass but how?

How can i add list in a generic class? Firstly My generic Class is that: [Serializable] public class ScheduleSelectedItems { private string Frequency; List FrequencyDays = new List(); private string Time; private string StartTime; private string EndTime; private string StartDate; ...

Can I use a collection initializer for Dictionary<TKey, TValue> entries?

I want to use a collection initializer for the next bit of code: public Dictionary<int, string> GetNames() { Dictionary<int, string> names = new Dictionary<int, string>(); names.Add(1, "Adam"); names.Add(2, "Bart"); names.Add(3, "Charlie"); return names; } So typically it should be something like: return new Dicti...

What advantage do you get with a collection over List(Of T) in .NET 2.0+

I had another developer ask why I use List all over the place ... and I thought about it for a minute ... and couldn't come up with a definitive answer. If you inherit from the collection base class to extend instead of using List(Of T) - what advantages do you get? Or - what don't you get with List? ...

ToString() does not return the expected string

using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3Generics { class Program { static void Main(string[] args) { ScheduleSelectedItems sitems = new ScheduleSelectedItems("Yusuf"); ScheduleSelectedItemsList slist = new ScheduleSelectedItemsList()...

Best way to pass Entity Framework entity collections from the DAL to the Business Layer?

I have a DAL that makes calls to my Entity Framework Model. The calls return things like IOrderedQueryable and IQueryable objects. Should I convert them to something more universal in my DAL, such as List? But I assume that if I don't need to enumerate through the entire collection, this could be wasteful... So whats the best approac...

Generics using public interfaces and internal type parameters

I have the following situation: // A public interface of some kind public interface IMyInterface { int Something { get; set; } } // An internal class that implements the public interface. // Despite the internal/public mismatch, this works. internal class MyInternalConcrete : IMyInterface { public int Somet...

Generics List with Array return but how ?

These Codes really boring. And Tostring() give me error !!! Can you rearrange these codes ? class Program { static void Main(string[] args) { string[] arraystr = { "yusuf", "mehmet" }; Ilist myitems = new Ilist(arraystr); SelectedItemsList slist = new SelectedItemsList(); ...

Why doesn't Java allow generic subclasses of Throwable?

According to the Java Language Sepecification, 3rd edition: It is a compile-time error if a generic class is a direct or indirect subclass of Throwable. I wish to understand why this decision has been made. What's wrong with generic exceptions? (As far as I know, generics are simply compile-time syntactic sugar, and they will be t...

Calling a generic interface

public interface IProcessor<T> { void Process(T instance); } foreach(AbstractType instance in myClass.SomeCollection) OnProcess(instance); public void OnProcess<T>(T instance) { IProcessor<T> processor = unityContainer.Resolve<IProcessor<T>>(); processor.Process(instance); } The problem with this code is that the in On...

Type erasure, overriding and generics

Can someone explain to me why @Override public void fooMethod(Class<?> c) doesn't override public void fooMethod(Class c) and gives me the following errors instead: - Name clash: The method fooMethod(Class<?>) of type SubClass has the same erasure as fooMethod(Class) of type SuperClass but does not override it - The method f...

Generics in Java, using wildcards

Hello, I have a question about Generics in Java, namely using wildcards. I have an example class GenClass like this: public class GenClass<E> { private E var; public void setVar(E x) { var = x; } public E getVar() { return var; } } I have another simple class: public class ExampleClass { } I have ...

I have a problem with IComparable and the collection sort method.

Okay so I have a scenario similar to the below code, I have a parent class that implements IComparable and a child class. class Parent : IComparable<Parent> class Child : Parent Child a = new Child(); Child b = new Child(); a.CompareTo(b); Now the above works fine, i can compare two of the child objects to each other no problem L...

How do I mock a method with generic objects in JMockit?

This question is self explanatory if you know how to use JMockit: How do I mock a method that has generics on it? I want to mock this method: public T save(T entity) but it always throws an exception like this: mockit.RealMethodNotFoundForMockException: Corresponding real methods not found for the following mocks: Object save(Object) ...

Trouble with type inference in writing generic extension method

I really hate sometimes how IDictionary<TKey, TValue> [key] will throw an exception if the key doesn't exist in the dictionary. Of course there is TryGetValue(), but that seems to have been optimized for performance and not usability. So I thought, oh I'll just make an extension method for it - which I did : public static class Collec...

How do I address unchecked cast warnings?

Eclipse is giving me a warning of the following form: Type safety: Unchecked cast from Object to HashMap<String, String> This is from a call to an API that I have no control over which returns Object: HashMap<String, String> getItems(javax.servlet.http.HttpSession session) { HashMap<String, String> theHash = (HashMap<String, String...