When I use the "object" in C # /. NET?
Honestly, it often doesn’t happen that you really need it. But when you do this, it is pretty obvious. Reflection, type type generation, serialization, or tagging is one of those that ends up around the object.
This is similar to void* ... which is essentially the same thing. You are wondering what to use for such a “rough” object until you find yourself in a corner without an exit, but to use it.
This is the lowest level you can find in managed code. All this is an object; you cannot go deeper.
General casting:
public T AddComponents<T>() { Component component = (Component)Activator.CreateInstance(typeof(T)); if (component != null) { component.Parent = this; components.Add(component); } return (T)(object)component; //Cannot directly cast Component to T since we have no constraint between them. } "Tags" of the idea of assigning an element to a universal container that has no idea about the contents:
public class DragDropWrapper { private object tag; public object Tag { get { return tag; } } } What drags on? No idea. It could be anything.
Or the most common event sender:
public void Message(object sender, string text) { Entry entry = new Entry(sender, EntryType.Message, text); AddEntry(entry); } The sender can be any.
Reflection for extending an object property:
public static List<InfoNode> ExpandObject(InfoGrid grid, InfoNode owner, object obj) { List<InfoNode> nodes = new List<InfoNode>(); if (obj == null) return nodes; PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo info in infos) nodes.Add(new PropertyNode(grid, owner, obj, info)); nodes = nodes.OrderBy(n => n.Name).ToList(); return nodes; } And many more features.
One of them rarely uses an object because it has so little functionality. It was used more often in V 1.0 (before generics), but now it is rarely used for much more than a random doble, like this:
(A)(object)someType; to get around the limitations of casting operations.
The real purpose of an object is to provide for the implementation of basic operations such as GetType() and ToString() , and to ensure that they are implemented by default.
Usually you will mainly use the type of an object only explicitly when interacting with the APIs that were written before the generics are added to C #.
One use case that I find fairly common is the following idiom:
class NeedsToBeThreadSafe { private static object _lock = new object(); void ThreadSafeOperation() { lock (_lock) { // Perform some useful work } } } Here we need the _lock object only to implement the lock; it does not need any members or the & mdash value, so the object type is suitable.
The object is the parent type for all types. Each individual type that you create or use from a .Net structure is inherited from it. For generics introduced in .Net, most collection types (for example, ArrayList in System.Collecrtions) are defined to accept object types. What this means is that you can add a mixed package of different types. And you need to bring elements to a specific type during extraction. This is unnecessary overhead.
To answer your question. You should avoid using the object anywhere, as it does not benefit your code. The whole point of introducing generics was to get rid of object notation, so you can avoid unnecessary throws in your code.
Ok, one very good example. Suppose you wanted to write a library, and this library should have been able to take an arbitrary piece of data and save it. For example, as in ORM, you can write such a function.
bool InsertObject(object item); The same can be said about the seralizer of the object. think json.
Basically, you would "use" an object when you need to work in a very general way.
Since object can be used to refer to any value, you use it when you need to do just that.
The String.Format method, for example, uses the object parameters to be able to put any value in a string:
public string Format(string format, params object[] parameters) This allows you to send any values to it, for example:
String.Format("{0} {1}", 1, "2"); Using it as a general type, for example, in a List<object> , is not as common as usual, you usually have a more specific type that you can use. However, the List<object> will be the general equivalent of the ArrayList class, which was often used before generics were introduced in .NET.
Object can be used when the API you are creating does not care about the base type or can accept everything . For example, I want to create a library that serializes everything. In this case, the library methods receive an instance of Object and can process the rest through reflection.
Or imagine you want to create a refactor-friendly method that receives an expression lambda-member-access, for example:
f(something, s => s.member); And you do not care what type of member can be, it can be anything. You can define a method, for example:
void f(SomeType something, Expression<Func<SomeType, object>>) ... Another common occurrence is to use it as a thread-blocking variable that other people have talked about.