Why .Net doesn’t fully support Generics?
As we know, purposes of generics introduced in .NET 2.0 are to avoid boxing and unboxing. Generics will be improve our application performance and make it safer. I will not dwell on generics functionality. It’s have more specific topic.
I will talk about .NET supports in generics. I’m already making simple code to make generics collection.
TList<Orders> list=DataRepository.OrdersProvider.GetAll();
OK, list is a generic collection from Orders domain object. In UI layer I need to bind this object. Fortunately, .NET 2.0 API support object as data source. In this case I use windows form as UI so I will be use BindingSource class.
private BindingSource ordersBindingSource;
ordersBindingSource.DataSource = list;
BindingSource support generics data source, but it’s self isn’t generics control. When I see the metadata, I found this.
public object DataSource { get; set; }
Why BindingSource doesn’t have generics data source property? For example like this
public List<T> DataSource { get; set; }
Hemm, when I bind my generics object to BindingSource it will be boxing it to object. Also when I need the current object, It will be unboxing again to my domain object.
Orders order =(Orders) ordersBindingSource.Current;
orderid = order.OrderID;
Might I have very little knowledge in .NET, so I don’t know how to avoid boxing and unboxing in this case. If you have any solution, it will be appreciated. CMIIW