C#object
C#object
Article • 09/17/2021
A class or struct definition is like a blueprint that specifies what the type can do. An object
is basically a block of memory that has been allocated and configured according to the
blueprint. A program may create many objects of the same class. Objects are also called
instances, and they can be stored in either a named variable or in an array or collection.
Client code is the code that uses these variables to call the methods and access the public
properties of the object. In an object-oriented language such as C#, a typical program
consists of multiple objects interacting dynamically.
7 Note
Static types behave differently than what is described here. For more information, see
Static Classes and Static Class Members.
Instances of classes are created by using the new operator. In the following example,
Person is the type and person1 and person2 are instances, or objects, of that type.
using System;
class Program
{
static void Main()
{
Person person1 = new Person("Leopold", 6);
Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name,
person1.Age);
Because structs are value types, a variable of a struct object holds a copy of the entire
object. Instances of structs can also be created by using the new operator, but this isn't
required, as shown in the following example:
using System;
namespace Example
{
public struct Person
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
The memory for both p1 and p2 is allocated on the thread stack. That memory is reclaimed
along with the type or method in which it's declared. This is one reason why structs are
copied on assignment. By contrast, the memory that is allocated for a class instance is
automatically reclaimed (garbage collected) by the common language runtime when all
references to the object have gone out of scope. It isn't possible to deterministically
destroy a class object like you can in C++. For more information about garbage collection
in .NET, see Garbage Collection.
7 Note
The allocation and deallocation of memory on the managed heap is highly optimized
in the common language runtime. In most cases there is no significant difference in
the performance cost of allocating a class instance on the heap versus allocating a
struct instance on the stack.
To determine whether two class instances refer to the same location in memory
(which means that they have the same identity), use the static Object.Equals method.
(System.Object is the implicit base class for all value types and reference types,
including user-defined structs and classes.)
To determine whether the instance fields in two struct instances have the same values,
use the ValueType.Equals method. Because all structs implicitly inherit from
System.ValueType, you call the method directly on your object as shown in the
following example:
if (p2.Equals(p1))
Console.WriteLine("p2 and p1 have the same values.");
// Output: p2 and p1 have the same values.
To determine whether the values of the fields in two class instances are equal, you
might be able to use the Equals method or the == operator. However, only use them
if the class has overridden or overloaded them to provide a custom definition of what
"equality" means for objects of that type. The class might also implement the
IEquatable<T> interface or the IEqualityComparer<T> interface. Both interfaces
provide methods that can be used to test value equality. When designing your own
classes that override Equals , make sure to follow the guidelines stated in How to
define value equality for a type and Object.Equals(Object).
Related Sections
For more information:
Classes
Constructors
Finalizers
Events
object
Inheritance
class
Structure types
new Operator
Common Type System
6 Collaborate with us on
GitHub .NET feedback
.NET is an open source project. Select a
The source for this content can
link to provide feedback:
be found on GitHub, where you
can also create and review issues
and pull requests. For more Open a documentation issue
information, see our contributor
guide. Provide product feedback