we will start object oriented programming (OOP) in C#. We will start with learning classes , objects and their basics. Then we will move to constructors, access modifiers, properties, method overloading and static methods.
Concepts of Class
A class is simply an abstract model used to define a new data types. A Class may contain any combination of encapsulated data ( fields or member variables), operations that can be performed on data (methods) and accessors to data (properties).
Example:-
class MyClass
{
// fields, operations and properties go here
}
where My Class is the name of class or new data type that we are defining here.
Objects
An object is an instance of a class. it has its own state, behavior and identity.
MyClass myobjectReference = new Myclass ();
Student Class String studentName int studentRollNum |
Student Object1 Student Object2 Student Object3
StudentName = “abc” StudentName = “pqr” StudentName = “xyz”
StudentRollNum = 1 StudentRollNum = 2 StudentRollNum = 3
Fields
Fields are the data contained in the class. Fields may be implicit data types, objects of some other class.
class student
{
// fields contained in student class
string name;
int age;
int marksInMaths;
int marksInEnglish;
int marksInScience;
int totalMarks = 300; // initialization
int ObtainedMarks;
double Percentage;
You can also initialize the fields with the initial values as we did in totalMarks in the example above. If you don’t initialize the members of the class, they will be initialized with their default values.
Default values for different data types are shown below:
Data Type | Default Value |
int | 0 |
long | 0 |
float | 0.0 |
double | 0.0 |
bool | False |
char | ‘\0’ (null character) |
string | “” (empty string) |
Objects | null |
Methods
Methods are the operations performed on the data. A method may take some input values through its parameters and may return a value of a particular data type.
To use a method, you need to –
- Define the method
- call the method
Defining Methods in C#
When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows –
<Access Specifier> < Return Type> <Method Body>
}
Following are the various elements of a method –
- Access Specifier – This determines the visibility of a variable or a method from another class.
- Return Type – A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is Void.
- Method Name – Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.
- Parameter List – Enclosed between parentheses , the parameters are used to pass and receive data from a method. The parameter list refers to the type, order and method. Parameters are optional; that is a method may contain no parameters.
- Method body – This contains the set of instructions needed to complete the required activity.
<return type> < name of methods> (<data type><identifier>, <data type> <identifier>,….)
{
// body of the method
}
For Example,
int FindSum ( int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
Calling Methods in C#
you can call a method using the name of the method. The following example illustrates this –
using system;
namespace calculatorApplication {
class NumberMainpulator {
public int FindMax (int num1, int num2)
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2
return result;
}
static void Main ( string []args) {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
NumberManipulator n = new NumberManipulator();
// calling the FindMax method
ret = n. FindMax ( a, b );
Console.WriteLine (” Max value is : {0}”, ret );
Console.ReadLine ( );
}
}
}
When the above code is compiled and executed , it produces the following result –
Max Value is : 200