Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
Object, in C#, is an instance of a class that is created dynamically. Object is also a keyword that is an alias for the predefined type System.Object in the .NET framework.
The unified type system of C# allows objects to be defined. These can be user-defined, reference or value type, but they all inherit directly or indirectly from System.Object. This inheritance is implicit so that the type of the object need not be declared with System.Object as the base class.
In general, object type is useful where there is a requirement to build generic routines. Because values of any type can be assigned to variables of object type, object type is used mostly in designing classes that handle objects of any type that allow code to be reused. The non-generic collection classes in the .NET framework library, such as ArrayList, Queue, etc., use object type to define various collections.
An object is also known as instance.
An object consists of instance members whose value makes it unique in a similar set of objects.
All the objects used in C# code are of object type.
When an object is instantiated, it is allocated with a block of memory and configured as per the blueprint provided by the class underlying the object. Objects of value type are stored in stack, while those of reference type are allocated in the heap.
Since the execution of C# code is in the managed environment of .NET, wherein the garbage collector provides automatic memory reclamation, it is not necessary or possible to explicitly de-allocate memory that is allocated for objects. Objects of value type are destroyed when they go out of scope, while reference type objects are destroyed in a non-deterministic manner until the last reference to them is removed.
The two operations related to objects created in C# are boxing and unboxing. While boxing implies the conversion of value type to object, unboxing refers to the conversion from object to a value type. Boxing and unboxing operations need to be used carefully because they can put a drag on performance.