new operator

In this chapter you will learn:

  1. How to use new operator
  2. Use new operator with value objects
  3. Object instance for class

Get to know new operator

The new operator has this general form:

class-var = new class-name();
  • class-var is a variable of the class type being created.
  • class-name is the name of the class that is being instantiated.

The class name followed by parentheses specifies the constructor for the class.

new with value object

using System; /* ja  v a  2 s .c o  m*/
 
class MainClass {  
  public static void Main() {  
    int i = new int(); // initialize i to zero 
 
    Console.WriteLine("The value of i is: " + i); 
  }  
}

The code above generates the following result.

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;

The code above 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.

Next chapter...

What you will learn in the next chapter:

  1. How to add fields to a class
  2. How to declare more than one fields
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances