Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0151 Class object instance

Creating Objects A class defines a type of object, but it is not an object itself. An object is an instance of a class. Objects can be created by using the new keyword followed by the name of the class. Rectangle object1 = new Rectangle(); object1 is a reference to an object based on Rectangle. object1 refers to the new object but does not contain the object data itself. In fact, you can create an object reference without creating an object at all: Rectangle object2; A reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, such as this: Rectangle object3 = new Rectangle(); Rectangle object4 = object3; This code creates two object references that both refer to the same object. Therefore, any changes to the object made through object3 will be reflected in subsequent uses of object4. Because objects that are based on classes are referred to by reference, classes are known as reference types.