0% found this document useful (0 votes)
2 views

CS Notes 6

The document explains the concepts of classes and objects in C#, fundamental to Object-Oriented Programming. It details how to declare a class, including its components such as modifiers, class identifier, base class, interfaces, and body, and provides examples of class and object instantiation. Additionally, it describes the characteristics of objects, including state, behavior, and identity, and illustrates object initialization using a Dog class example.

Uploaded by

devil289wl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS Notes 6

The document explains the concepts of classes and objects in C#, fundamental to Object-Oriented Programming. It details how to declare a class, including its components such as modifiers, class identifier, base class, interfaces, and body, and provides examples of class and object instantiation. Additionally, it describes the characteristics of objects, including state, behavior, and identity, and illustrates object initialization using a Dog class example.

Uploaded by

devil289wl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C# Class and Objects

Class and Object are the basic concepts of Object-Oriented Programming


which revolve around real-life entities. A class is a user-defined blueprint or
prototype from which objects are created. Basically, a class combines the
fields and methods(member functions which define actions) into a single
unit. In C#, classes support polymorphism, and inheritance and also provide
the concept of derived classes and base classes.
Declaration of Class in C#
Generally, A class declaration contains only a keyword class, followed by
an identifier(name) of the class. However, some optional attributes can be
used with class declaration according to the application requirement
Class declarations can include these components, in order:
 Modifiers: Used to define accessibility levels by default modifier of the
class is internal.
 Keyword class: A class keyword is used to declare a class.
 Class Identifier: The variable of type class is provided. The
identifier(name of the class) and the initial letter should be capitalized by
convention.
 Base class(Optional): If it inherits its parent class(superclass), if any,
then preceded by the : (colon).
 Interfaces(Optional): A comma-separated list of interfaces
implemented by the class, if any, preceded by the : (colon). A class can
implement more than one interface.
 Body: The class body is enclosed by { } (curly braces) and it may
contain the class’s members like properties, methods, constructors
events etc.
Example:
// Declaration of class
using System;
// declaring public class
public class myclass
{
// field variable
public int a, b;

// member function or method


public static void display()
{
Console.WriteLine("Class & Objects in C#");
}

public static void Main(String[] args)


{
display();
}
}

Output
Class & Objects in C#

Objects in C#
Object is a basic unit of Object-Oriented Programming and represents real-
life entities. It can also refer to the instance of a class. An object of that
class is created. An object holds data and can access methods and their
properties. In C# an object consists of :
 State: It is represented by attributes of an object, and reflects the
properties of an object.
 Behaviour: It is represented by the methods of an object, and also
reflects the response of an object with other objects.
 Identity: It gives a unique name to an object and enables one object to
interact with other objects.
 Objects correspond to things found in the real world. For example, a
graphics program may have objects such as “circle”, “square”,
“menu”. An online shopping system might have objects such as
“shopping cart”, “customer”, and “product”.
Declaring Objects (Also called instantiating a class)
When an object of a class is created, the class is said to be instantiated. All
the instances share the attributes and the behavior of the class. But the
values of those attributes, i.e. the state are unique for each object. A single
class may have any number of instances.
Initialization of Object
The new keyword instantiates a class by allocating memory for a new
object and returning a reference to that memory. The new operator also
invokes the class constructor.
// Initialization of an object
using System;

// Class Declaration
public class Dog
{

// Instance Variables
String name;
String breed;
int age;
String color;

// Constructor Declaration of Class


// same name as class
public Dog(String name, String breed,
int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}

public String GetName()


{
return name;
}

public String GetBreed()


{
return breed;
}

public int GetAge()


{
return age;
}

public String GetColor()


{
return color;
}

public override String ToString()


{
return "my name is: " + name +
"\nmy breed is: " + breed +
"\nmy age is: " + age;
}

public static void Main(String[] args)


{
// Creating object
Dog tuffy = new Dog("tuffy", "papillon", 5, "white");
Console.WriteLine(tuffy.ToString());
}
}
Output
my name is: tuffy

my breed is: papillon

my age is: 5

When we create an object of the Dog class and pass the parameters in the
constructor. So it allocates memory for these different objects and their
address points with the class’s object as shown in the image.

You might also like