Chapter 2
WHAT ARE CLASSES AND OBJECTS AND METHODS IN C#
Classes and objects are the basic building blocks of any object oriented progranning language.
the classes may refer to some real world entity which is used to instantiate into objects.
Every programming language provides us with the ability to declare a variable of a particular data type like int, float etc, which we later use to store data and perform calculations or apply logic into.
Similarly , the classes can be considered as a data type, which will be defined by the user or the programmer himself (a user defined datatype) and the Objects of this class can be refered to as a variable of this newly created datatype. hence , based on the requirement the programmer creates a class representing a real world entity like an employee class or a book class. and this can be instantiated to objects of any number. these objects can be used later to store data into them or any logic can be applied on them.
A class may contain the following members , if not all of them:
Example of a simple class,
using System;
namespace Logiphix
{
public class Calculator
{
public int numOne;
public int numTwo;
public int result;
public calculator()
{
}
public void Add(int _one, int _two )
{
numOne = _one;
numTwo = _two;
result = numOne + numTwo;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
}
class Program
{
static void Main()
{
Calculator cObj = new Calculator();
cObj.Add(1,2);
}
}
}
Here in the above example, we can see that we have created a class named Calculator this class has a three variables inside it of integer type namely numOne , numTwo, result the basic functionality of this class is that it performs the operation of a claculator, thus replicating a real world entity.
The class has a constructor defined
public calculator()
{
}
this constructor is called when the object is created , more on constructors will be explained later.
There is a method defined inside the class called Add
public void Add(int _one, int _two )
{
numOne = _one;
numTwo = _two;
result = numOne + numTwo;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
This method takes two parameters, these parameters are passed as values to the method when it is called. the method Add claculates the sum of the two values(integers) and prins the result on the console.
More on methods and constructors:
Constructors
A Constructor is a class method that is executed when a object of a class or a struct is created, the constructor has the same name as of the class and has no return type.
Structure of a constructor:
public ClassName()
{
}
A class can have multiple constructors usually with different parameter signatures. this property is called overloading.
for example,
We can have the different constructor types as below:
public calculator()
{
// perform initialization here
}
or
public calculator( int a, int b)
{
// perform initialization here
}
or
public calculator(double a , double b)
{
// perform initialization here
}
etc
It is not mandatory to define a constructor in C#. if no constructors are defined the compiler assignes a default constructor to the class , this default constructor has no parameters.
Constructors are usually used to initialize fields of the class, and to perform initial operations for the class. Static classes and structs can also have constructors.
Methods
A method is a code block containing a series of statements. In C#, every executed instruction is done so in the context of a method
For example,
public void Add(int _one, int _two )
{
numOne = _one;
numTwo = _two;
result = numOne + numTwo;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
the above code block shows the structure of a method. it has a access specifier(access level) followed by a return type aand the name of the method followed by the parameters.
Methods are declared inside a class or a structure.
A single calss can have multiple methods with the same name, but different parameter signatures, this phenoenon is called method overloading
For Example,
public void Add(double _one, double _two )
{
double result;
result = _one + _two;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
in the above method declaration the parameters are of type double, and calculate the addition of two double data type parameters.
Methods can return a value to the caller, the caller is the entity that invoked this method. The return of the value is done by the keyword "return" followed by the returned value.
Example:
public double Add(double _one, double _two )
{
double result;
result = _one + _two;
// return the result to the caller
return result;
}
if the return type is void , then no value is returned , but the keyword return can still be used but without a returned value following it.
for example,
public void Add(double _one, double _two )
{
double result;
result = _one + _two;
//return to the caller
return;
}
WHAT ARE CLASSES AND OBJECTS AND METHODS IN C#
Classes and objects are the basic building blocks of any object oriented progranning language.
the classes may refer to some real world entity which is used to instantiate into objects.
Every programming language provides us with the ability to declare a variable of a particular data type like int, float etc, which we later use to store data and perform calculations or apply logic into.
Similarly , the classes can be considered as a data type, which will be defined by the user or the programmer himself (a user defined datatype) and the Objects of this class can be refered to as a variable of this newly created datatype. hence , based on the requirement the programmer creates a class representing a real world entity like an employee class or a book class. and this can be instantiated to objects of any number. these objects can be used later to store data into them or any logic can be applied on them.
A class may contain the following members , if not all of them:
- Fields
- Constructors
- Methods
- Destructors
- Properties
- Delegats
- Events
- Nested classes
Example of a simple class,
using System;
namespace Logiphix
{
public class Calculator
{
public int numOne;
public int numTwo;
public int result;
public calculator()
{
}
public void Add(int _one, int _two )
{
numOne = _one;
numTwo = _two;
result = numOne + numTwo;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
}
class Program
{
static void Main()
{
Calculator cObj = new Calculator();
cObj.Add(1,2);
}
}
}
Here in the above example, we can see that we have created a class named Calculator this class has a three variables inside it of integer type namely numOne , numTwo, result the basic functionality of this class is that it performs the operation of a claculator, thus replicating a real world entity.
The class has a constructor defined
public calculator()
{
}
this constructor is called when the object is created , more on constructors will be explained later.
There is a method defined inside the class called Add
public void Add(int _one, int _two )
{
numOne = _one;
numTwo = _two;
result = numOne + numTwo;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
This method takes two parameters, these parameters are passed as values to the method when it is called. the method Add claculates the sum of the two values(integers) and prins the result on the console.
More on methods and constructors:
Constructors
A Constructor is a class method that is executed when a object of a class or a struct is created, the constructor has the same name as of the class and has no return type.
Structure of a constructor:
public ClassName()
{
}
A class can have multiple constructors usually with different parameter signatures. this property is called overloading.
for example,
We can have the different constructor types as below:
public calculator()
{
// perform initialization here
}
or
public calculator( int a, int b)
{
// perform initialization here
}
or
public calculator(double a , double b)
{
// perform initialization here
}
etc
It is not mandatory to define a constructor in C#. if no constructors are defined the compiler assignes a default constructor to the class , this default constructor has no parameters.
Constructors are usually used to initialize fields of the class, and to perform initial operations for the class. Static classes and structs can also have constructors.
Methods
A method is a code block containing a series of statements. In C#, every executed instruction is done so in the context of a method
For example,
public void Add(int _one, int _two )
{
numOne = _one;
numTwo = _two;
result = numOne + numTwo;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
the above code block shows the structure of a method. it has a access specifier(access level) followed by a return type aand the name of the method followed by the parameters.
Methods are declared inside a class or a structure.
A single calss can have multiple methods with the same name, but different parameter signatures, this phenoenon is called method overloading
For Example,
public void Add(double _one, double _two )
{
double result;
result = _one + _two;
console.WriteLine( "The sum is : " + result.toString());
console.ReadKey();
}
in the above method declaration the parameters are of type double, and calculate the addition of two double data type parameters.
Methods can return a value to the caller, the caller is the entity that invoked this method. The return of the value is done by the keyword "return" followed by the returned value.
Example:
public double Add(double _one, double _two )
{
double result;
result = _one + _two;
// return the result to the caller
return result;
}
if the return type is void , then no value is returned , but the keyword return can still be used but without a returned value following it.
for example,
public void Add(double _one, double _two )
{
double result;
result = _one + _two;
//return to the caller
return;
}
No comments:
Post a Comment