8.1. Introduction To Classes: Class Classname (//code Here)
8.1. Introduction To Classes: Class Classname (//code Here)
8.1. Introduction To Classes: Class Classname (//code Here)
Introduction to Classes
We know Class describes the state & behaviour of objects. Thus a Class decides/ defines what an object does and what data it holds.
After creating/ designing a Class, any number of similar objects can be created.
Thus, a class becomes the template for an object .An object becomes the instance of its class.
class ClassName
//code here
Class Customer
The variables declared inside a Class are by default not accessible from other Classes, but to access the values from outside,properties
are used.
The properties are special methods called as Accessors, which allows the data to be assigned to/ accessed from the variable, through
Read-only/ Write-only/ Read-Write permissions. The properties enable a class to expose only the getting & setting of values, while
The property contains 2 blocks, a get block, which is called Get accessor & set block, which is called Set accessor. When the data
has to be read using the property, the get accessor will work & when the data has to be assigned to, the set accessor will work.
Thus the property restricts the access to the value. Property containing only the get block/ get accessor is a Read-Only Property. A
property containing only the set block/ set accessor is the Write-Only property, the Property containing both the Get & Set Accessor is a
Read-Write Property.
For e.g.; we will include the properties for Id & name in our customer class. The Id of the customer is restricted from modification
whereas the name can be given with Read-Write Permission. Our Class will look like as follows:
class Customer
{
{
}
//Declare Variable for name & use Property for Read & Write
{
}
In C#, the variables inside a class are initialized with default values using a constructor. A constructor should have the same name as
{
}
Since the constructor needs to be accessed from other Classes, the Access modifier will always be public (most permissive).
For e.g.; we can include a Constructor also in our class Customer, for initializing the Id and name of any customer. Now, our class will
look like:
class Customer
{
{
}
//Declare Variable for name & use Property for Read & Write
{
}
//Constructor for passing the initial value(as parameter) to the variables
}
}
A class can have multiple constructors. Also, if a class does not contain a constructor, C# will instantiate the default values using a
default Constructor.
C# support object- Oriented Programming Approach, which include : Encapsulation, inheritance & Polymorphism.
Encapsulation: A group of related members treated as a single unit. A Class is a single unit which keeps the variables, its related
properties, constructors & other members inside it. Here the members are encapsulated within the Class.
For e.g; our customer class holds some members. Using Encapsulation, all the members are treated as a single unit using the class
name Customer.
A class defines the type of object. So the object will have the type as the Class name. And the object is created using the “new”
keyword followed by the constructor. The constructor is invoked by the new operator.
The syntax of creating an object using a default constructor for a Class with name ClassName, is as follows:
Here, ClassName(newValue) – is used to invoke the parameterized Constructor taking only one value as the parameter and newValue
For e.g.; we will see how to create an object of our class customer, using a default Constructor:
In C#, Console Application or a Windows Application needs to have starting point for the program. The Main Method is defined as the
entry point of a “.exe” program, i.e. the program starts execution from the Main Method.
One class should contain a Main Method. Since the execution starts from the Main Method and it is invoked without creating an
object of the Class, the Main method should be defined using the "static" keyword.
Main method should also specify a return type. The return type can be either "void" type which means returns nothing or “int”
datatype which means returns an integer value by the completion of Main method.
Main Method can be defined with or without parameters. The Parameter are used to take the Command line Arguments and the
{
}
or
{
}
Here we will see the Class program with the Main Method creating the objects of class customer and using the members defined inside.
class Program
{
{
customer.Name="Joe";
Console.ReadKey();
}
}
class Customer
{
{
}
//Declare Variable for name & use Property for Read & Write
{
}
//Constructor for passing the initial value(as parameter) to the variables
{
}
{
//
}
}
Output:
0 :: Joe
100 :: Jack
2. Variable Naming Convention - Variable names can be short, but need to be meaningful for the purpose. All variable names can
start with a lower case first letter and the next word in the same start with capital letter. Variable name should be mnemonic — that is,
designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided.
3. Property Naming Convention – Property names are given Similar as that of corresponding member, in pascal casing.
4. Comments – In C#, single line Comments are given using // & multi-Line comments are given using /* and */.
Write a class with a main method that will print you the Name, Designation, and Place on the console.
output should be
Name: Jacquil
Designation: Consultant
Place: LA
9O9 is a web shoppe which helps the user to purchase their item in a convenient manner. But to order the items the user need to
register first and then generate an online Bill after the internet banking.
In this scenario, you might have identified some objects, what are they? What is the name of the Class chosen for the Registration
The objects that we can find here are User, Item, Bill. We need an object for the user for the registration details.
To create an object for the user, we need a class with name User. We need variables for taking the user details like First Name, Last
Name, age and Location and Email ID. We will be passing the values to the user object during the time of object creation, so we need a
To start with the execution and work with the User class, we will include a Program class with Main method.
Test the User registration by using 3 user objects. Print the name of the user using the objects created.
Give the details of users like Haris Jack . Print the Last name, First name of the user as output. I.e;
Jack, Haris
Solutions:
class User
//Variables
//Constructors
public User()
//
public User(string firstname, string lastname, int age,string location, string email)
{
_firstname = firstname;
_lastname = lastname;
_age = age;
_location = location;
_email = email;
//Properties
get
return _firstname;
set
_firstname = value;
get
return _lastname;
set
{
_lastname = value;
get
return _age;
set
_age = value;
get
return _location;
set
_location = value;
}
public string EmailId
get
return _email;
set
_email = value;
class Program
Console.Write(user1.LastName);
Console.Write(", ");
Console.WriteLine(user1.FirstName);
user2.FirstName = "Robert";
user2.LastName = "George";
user2.Age = 60;
user2.Location = "LA";
user2.EmailId = "[email protected]";
Console.Write(user2.LastName);
Console.Write(", ");
Console.WriteLine(user2.FirstName);
Console.Write(user1.LastName);
Console.Write(", ");
Console.WriteLine(user1.FirstName);
Console.ReadKey();
Output:
Jack, Haris
George, Robert
Joe
Wilson
50
London
Wilson,Joe