Basics Of C Sharp C# - 2
Classes and Methods
The class is a template, declaration or blueprint
that is used for classifying the object. It encapsulates variable members,
functions, structure, properties and many more components. It is the basic
building block of object-oriented programming. In order to create a class, the
class keyword is used
Example : 1
Syantax
‘
class print
{
}
After creating class, you can use it
by creating its object. An object is created using new keyword. Suppose you
created a class print that contain following method.
class print
{
public void printname()
{
Console.WriteLine("My name is Steven Clark");
}
}
To use the members of class, you need
to create object of this class. After creating the object of print class, you
can use its members using the object name as follow:
print pr = new print();
pr.printname();
Example : 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Creating_Class
{
class accept //Creating 1st. class
{
public string name;
public void acceptdetails()
{
Console.Write("Enter your
name:\t");
name = Console.ReadLine();
}
}
class print // Creating 2nd class
{
public void printdetails()
{
//Creating object of 1st. class
accept a = new accept();
//executing method of 1st class.
a.acceptdetails();
//Printing value of name variable
Console.WriteLine("e;Your name
is "e; + a.name);
}
}
class Program //Creating 3rd class
{
static void Main(string[] args)
{
print p = new print();
p.printdetails();
Console.ReadLine();
}
}
}
- Guideliness while creating classes
o
The class name should be noun and
meaningful.
o
Use either pascal case or camel
case. In camel case, the first letter is small. Ex. camelCase. In pascal case
first letter is capital. Ex. PascalCase. It is strictly recommended you to use
pascal case for class name and camel case for variable name.
o
Your class name should not
contain any special character except underscore (_) or digit. Must start your
class name with character.
o
Don’t use reserved keyword for
class name.
Methods
Method is the building block of object-oriented programming. It combines
related code together and makes program easier. In C# method declaration, you
can declare method by following way:
<Access Specifier> <Return Type> <Method
Name>(Parameter list)
{
Body
}
Example
public void add()
{
Body
}
Example for Method
namespace Declaring_Method
{
class Program
{
string name, city;
int age;
// Creating method for
accepting details
public void acceptdetails()
{
Console.Write("\nEnter your name:\t");
name = Console.ReadLine();
Console.Write("\nEnter
Your City:\t");
city = Console.ReadLine();
Console.Write("\nEnter your age:\t\t");
age =
Convert.ToInt32(Console.ReadLine());
}
// Creating method for
printing details
public void printdetails()
{
Console.Write("\n\n===================");
Console.Write("\nName:\t" + name);
Console.Write("\nCity:\t" + city);
Console.Write("\nAge:\t" + age);
Console.Write("\n===================\n");
}
static void Main(string[]
args)
{
Program p = new Program();
p.acceptdetails();
p.printdetails();
Console.ReadLine();
}
}
}
Calling Method or Fuction
After creating
function, you need to call it in Main() method to execute. In order to call
method, you need to create object of containing class, then followed bydot(.) operator you can call the
method. If method is static, then there is no need to create object and you can
directly call it followed by class name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Declaring_Method
{
class Program
{
string name, city;
int age;
public void acceptdetails()
{
Console.Write("\nEnter your
name:\t");
name = Console.ReadLine();
Console.Write("\nEnter Your
City:\t");
city = Console.ReadLine();
Console.Write("\nEnter your
age:\t\t");
age =
Convert.ToInt32(Console.ReadLine());
}
public void printdetails()
{
Console.Write("\n\n====================");
Console.Write("\nName:\t"
+ name);
Console.Write("\nCity:\t"
+ city);
Console.Write("\nAge:\t"
+ age);
Console.Write("\n====================\n");
}
static void Main(string[] args)
{
//creating object of class Program
Program p = new Program();
p.acceptdetails(); // Calling
method
p.printdetails(); // Calling method
Console.ReadLine();
}
}
}
Static Methods and
Variables
Whenever you write a function or declare a
variable, it doesn’t create an instance in a memory until you create an object
of the class. But if you declare any function or variable with a static
modifier, it directly creates an instance in a memory and acts globally. The
static modifier doesn't reference any object.
namespace Static_var_and_fun
{
class number
{
// Create static variable
public static int num;
//Create static method
public static void power()
{
Console.WriteLine("Power of
{0} = {1}", num, num * num);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a
number\t");
number.num =
Convert.ToInt32(Console.ReadLine());
number.power();
}
}
}
WHY THE MAIN METHOD IS ALWAYS DECLARED WITH STATIC?
The Main method in C# is always declared with
static because it can’t be called in another method of function. The Main
method instantiates other objects and variables but there is no any method
there that can instantiate the main method in C#. On another hand, the main
method doesn’t accept parameter from any other function. It only takes a
parameter as an argument via command line argument.
Inheritance
Inheritance is the one of the
important pillar of Object Oriented Programming (OOP). In this chapter you will
understand Inheritance in simple and easy language with real world example.
Inheritance allows you to access
members of base class in child class. It enables you to create a child class
that can access and use all the functionality of its base class. This way you
can keep common variable and functions in a base class and use them as many
times as you want in child class. Code reusability makes your program simpler
and efficient.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Basic_Example
{
class Program
{
static void Main(string[] args)
{
Scooter sc = new Scooter();
sc.ScooterType();
Car c = new Car();
c.CarType();
Console.ReadKey();
}
}
//Creating Base Class
class Tyre
{
protected void TyreType()
{
Console.WriteLine("This is
Tubeless Tyre");
}
}
//Creating Child Class
class Scooter : Tyre
{
public void ScooterType()
{
Console.WriteLine("Scooter
Color is Red");
TyreType();
}
}
//Creating Child Class
class Car : Tyre
{
public void CarType()
{
Console.WriteLine("Car Type :
Ferrari");
TyreType();
}
}
}
WHICH TYPE OF MEMBER CAN BE ACCESSED
BY A CHILD CLASS OR DERIVED CLASS?
A child class
or derived class can access all the public, protected, internal and protected
internal member. Private member cannot be accessed by child class however it is
inherited and still present in child class and can be accessed using public
property (GET SET modifier). There are two examples that demonstrate all the
concept of member access clearly. First example will show which type of member
can be accessed in child class and another example will show how to access
private member in child class using GET SET modifier.
using System;
using System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
Member_Access
{
class Program
{
static void Main(string[] args)
{
childclass child = new
childclass();
child.checkmember();
Console.ReadKey();
}
}
class baseclass
{
public void public_member()
{
Console.WriteLine("I am Public
Method");
}
protected void protected_member()
{
Console.WriteLine("I am
Protected Method");
}
internal void internal_member()
{
Console.WriteLine("I am
Internal Method");
}
protected internal void
protected_internal_member()
{
Console.WriteLine("I am
protected internal method");
}
private void private_member()
{
Console.WriteLine("I am
private method");
}
}
class childclass : baseclass
{
public void checkmember()
{
public_member();
protected_member();
protected_internal_member();
internal_member();
//private_member(); //Raise Error.
It can't be accessed
}
}
}
How to access
private variable using get set properties
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
Member_Variable
{
class Program
{
static void Main(string[] args)
{
childclass ch = new childclass();
ch.check();
Console.ReadKey();
}
}
class baseclass
{
public int pub_var = 5;
protected int pro_var = 6;
internal int inter_var= 7;
private int pri_var = 8;
public int Private_variable
{
get
{
return pri_var;
}
set
{
pri_var = value;
}
}
}
class childclass : baseclass
{
public void check()
{
int sum = pub_var + pro_var +
inter_var + Private_variable;
Console.WriteLine("Total : " +
sum.ToString());
}
}
}
Comments
Post a Comment