When should I use Object in C # 2.0 and later? Does Generics replace all occurrences of an object?

My colleague claimed that when declaring variables, return parameters, etc. in .NET 2.0 and later, you never need to use Object .

He went further and said in all such cases, Generic should be used as an alternative.

Is there any justification for this requirement? At the top of my head, I use Object to block parallel threads ...

+7
source share
7 answers

Generics do trump object in many cases, but only where the type is known .

There are still moments when you do not know the type - object , or some other appropriate base type - this is the answer in these cases.

For example:

 object o = Activator.CreateInstance("Some type in an unreferenced assembly"); 

You will not be able to use this result or even know that the type is at compile time, so object is a valid use.

Your colleague generalizes too much - perhaps point him to this question. Generics are great, give it as much, but they do not "replace" the object .

+8
source

object perfect for locking. Generics allow you to save it accordingly. You can even restrict it to an interface or a base class. You cannot do this with object .

Consider this:

 void DoSomething(object foo) { foo.DoFoo(); } 

This will not work without casting. But with generics ...

 void DoSomething<T>(T foo) where T : IHasDoFoo { foo.DoFoo(); } 

With C # 4.0 and dynamic you can remove this at runtime, but I really didn't see the need.

 void DoSomething(dynamic foo) { foo.DoFoo(); } 
+3
source

When using interaction with COM, you do not always have a choice ... Generic cannot cope with interaction problems.

Object also the easiest option for lock , like @Daniel A. White mentioned in his answer .

+2
source

Yes, there is certainty. Good breakdown has already been done here .

However, I cannot confirm whether there is an instance in which you will never use objects, but I personally do not use them, and even before generics I avoided boxing / unpacking.

+1
source

There are many counterexamples, including the one you mentioned, using an object for synchronization.

Another example is the DataSource property used in data binding, which can be set to one of many different types of objects.

0
source

A wide counterexample: the System.Collections namespace is active and good in .NET 4, no signs of obsolescence or warning regarding its use on MSDN. The methods you find there accept and return objects.

0
source

There are actually two questions in the question:

  1. When to use `Object` storage locations
  2. When should instances of type `Object` be used

    A storage of type Object should obviously be used in all circumstances where it will be necessary to contain references to instances of this type (since references to such instances cannot be stored in any other type). In addition, they should be used in cases where they will contain references to objects that do not have a single useful common base type. This is obviously true in many Reflection scenarios (where the type of the object may depend on the string computed at runtime), but can also apply to some collection manifolds that are populated with things whose type is known at compile time. As a simple example, we could imagine a hierarchical set of string indexed by int sequences, having each node of type Object and having either a string or Object[] . Reading elements from such a collection would be somewhat awkward, since you would have to examine each element and determine whether it was an instance of Object[] or string , but this storage method would be extremely memory efficient, since the only object instances were those that either held strings or arrays. You can define a Node type with a field of type string and one of Node[] types, or even define an abstract Node type with derived types StringNode (including a field of type string ) and ArrayNode (with a field of type Node[] ), but such approaches would increase the number of heap objects used to store a given data set.

    Note that in general it is better to create collections so that the type of the returned object does not depend on what was embedded in the collection (possibly using "parallel collections" for different types), but not everything works, that way is semantical.

    As for instances of type Object , I'm not sure if they can fill any role that would not be equally satisfied with a sealed type called TokenObject , which inherits from Object . There are a number of situations where it is useful to have an instance of an object whose sole purpose is a unique marker. It is clear that it would be better to say:

      TokenObject myLock = new TokenObject;
    

    than say

      Object myLock = new Object;
    

    since in the first declaration it will be clear that the declared variable will never be used to store anything other than a token object. However, it is common practice to use instances of type Object in cases where the only thing that matters to the object is that its reference will be unique throughout the entire program life cycle.

0
source

All Articles