Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Object instead of generics

If I'm using non-generic collections, functions and so on then passed value types will be boxed because of casting to Object (that is not good for performance). But after including generics in C# 2.0 most situations of passing value type as Object was obviated.

Are there still any situations in competent programming where it's not possible to accomplish something with generics and still need to use casting to Object? If so can you please show an example? Thanks.

like image 452
kyrylomyr Avatar asked Jun 14 '26 08:06

kyrylomyr


1 Answers

The use of System.Object offers flexibility (at the cost of performance).

Consider the storage mechanisms of ASP.NET (Session and Application), they have the general structure of

 Dictionary<string, object> PropertyBag;

And you would use them like

PropertyBag["currentCustomer"] = aCustmer;
PropertyBag["visits"] = 0;

It's not possible to replace object with something stronger typed here.

like image 150
Henk Holterman Avatar answered Jun 15 '26 22:06

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!