C# class and objects
M.SARULATHAAssistant
ProfessorDept.
of CA, JJC.
Class and Object are the basic
concepts of Object-Oriised Programming that revolve around the realities of
life. A category is a user-defined plan or specific type of process. Basically,
a category combines fields and methods (the member's job description) into a
single unit. In C #, the classes support the polymorphism, inherit and provide
the concept of classes based on the basic categories.
Declaration of Class
Normally,
a class declaration contains only the keyword category, followed by the class
name (s). But there are other optional features that can be used with the class
announcement depending on the need for the app. Generally, section
announcements may include the following sections:
Conversion:
Category can be public or internal etc. By default the class conversion is
internal.
Keyword category:
A category keyword is used to announce a category category.
Class Identifier:
Category category variations are provided. The identifier (or category name)
should start with the first letter to be added by the meeting.
Base or Super class:
Name of the parent of the class (superclass), if any, followed by: (colon).
This can be selected.
Methods of communication:
A comma-separated list of intersections formed, if any, preceded by: (colon). A
category can do more than one collaboration. This can be selected.
Body: The body of the
class is surrounded by {} (curly braces).
Classroom builders are used to innovating. Fields
are flexible objects that give the class and its objects, and methods are used
to make the class and its objects work.
//
declaring public class
public class sample
{
// field
variable
public int a,
b;
// member function or method
public void
display()
{
Console.WriteLine(“Classes
in C#”);
}
}
Objectives
It is a basic unit of Object-Oriised Programming and
represents real objects. The standard C # system does a lot of things, as you
know, interacting with incoming routes. Item contains:
Status:
Represented by object symbols. It also shows the material of the object.
Behavior:
Represents means things. It also shows the reaction of an object and other
things.
Identity:
It gives a different name to an object and enables one object to interact with
other objects.
Look at the Dog as an object and see the diagram
below for its identity, status, and behavior.

The
objects correspond to the objects found in the real world. For example, a
graphical system may have elements such as "circle",
"square", "menu". The online shopping system may contain
items such as "shopping cart", "customer", and
"product".
Declaring Objects
(Also called section strengthening)
When
a class object is made, it is said that the section is strengthened. All
situations share the characteristics and behaviors of the category. But the
values of those attributes, e.g. The situation is different for each item.
One category may contain a number of cases.

As we announce the same variables (type name;). This
informs the compiler that we will use the word to refer to the data of its
type. For older variants, this declaration also retains the correct amount of
memory for variables. So flexible, the type should be a solid concrete name.
Dog puppy;
If we
declare a puppy variable like this, its value will not be determined (null) until
an object is created and assigned to it. Simply announcing the flexibility of
the reference does nothing.
It starts something
The
new function strengthens the stage by sharing the memory of something new and
restoring the reference to that memory. The new operator also requests a
category builder.
//
C# program to illustrate the
//
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
public
Dog(String name, String breed, int age, String color)
{
this.name
= name;
this.breed
= breed;
this.age
= age;
this.color
= color;
}
// Property 1
public
String getName()
{
return
name;
}
// Property 2
public
String getBreed()
{
return
breed;
}
// Property 3
public
int getAge()
{
return
age;
}
// Property 4
public
String getColor()
{
return
color;
}
// Method 1
public
String toString()
{
return
("Hi my name is " + this.getName() + ".\nMy breed, age and color
are " + this.getBreed() + ", " + this.getAge() + ", "
+ this.getColor());
}
//
Main Method
public static void Main(String[] args)
{
// Creating object
Dog
puppy = new Dog("puppy", "papillon", 5, "white");
Console.WriteLine(puppy.toString());
}
}
This
category contains one constructor. We can see the builder because its
announcement uses the same name and category and has no return type. The C #
compiler divides builders by number and type of arguments. The builder in the
Dog category takes on four issues. The following statement lists "puppy",
"papillon", 5, "white" as criteria for such disputes:
Comments
Post a Comment