how to use class in c#.Net in console application.
Object Oriented Programming.
Class :
A Class is a construct that enables you to create your own custom types, by grouping variables and methods and events together.
now this becomes a type for us which can be used in or projects.
Def :
A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class.
syntax of declaring a class:
class classname
{
variables
properties
methods
events
}
Here class is a keyword which is preceded by the access levels
We have different access levels used in our programming while constructing a class. they are 1) private
2) public
3) protected
4) internal
using this access specifiers we can block the access of elements inside the class.
Private: the data declared in private section cannot be access outside the class. we can use methods declared in public section or properties declared in public section to access these private data.
Public : the data declared in public section can be accessed any where in the code in that namespace.
protected : this is similar to private but data declared under protected can be used in the class which are inherited.
internal: the type of member can be accessed by any code in the same assembly (dll)
Class is a model :
we need to create an object in order to access the data inside the class.
an object is an instance of a class.
syntax to create an object from a class:
classname objectname = new classname();
to find the reference of class click on F12 on that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstClass
{
class Program
{
static void Main(string[] args)
{
student s = new student();
s.addstudentdata();
Console.WriteLine();
s.displaystudentdata();
Console.Read();
}
}
public class student
{
private
int rno, m1, m2;
protected
int total;
public void addstudentdata()
{
Console.WriteLine("Enter the student information : rno , m1, m2 :");
rno = Convert.ToInt32(Console.ReadLine());
m1 = Convert.ToInt32(Console.ReadLine());
m2 = Convert.ToInt32(Console.ReadLine());
}
public void displaystudentdata()
{
total = m1 + m2;
Console.WriteLine("Rno :" + rno);
Console.WriteLine("Total :"+total);
}
}
}
Write a program to create 5 student details using array objects.
Constructors :
Constructors are methods which are invoked automatically when an object of its type is created.
to define a constructor it has some rules.
1)Name of the constructor method should be same of class name
1)This constructor should be present in the public because, compiler would take responsibility to invoke this constructor method.
There are different types of constructors :
1) default constructor
2) parameterized constructor
3) copy constructor
4)private constructor
Default constructor would be defined in the program by default and it is not mandatory
parameterized constructor is one which has parameters in it
we will be using this keyword which refers to the current instance of the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstClass
{
class Program
{
static void Main(string[] args)
{
student s = new student(10,30);
Console.WriteLine();
s.displaystudentdata();
student D = new student(40,50);
Console.WriteLine();
D.displaystudentdata();
Console.Read();
}
}
public class student
{
private
int rno, m1, m2;
protected
int total;
public student()
{
rno = 100;// if you want to initialize the default values they should be coded here.
}
public student(int mark1, int m2)
{
m1 = mark1;
this.m2 = m2;
}
public void displaystudentdata()
{
total = m1 + m2;
Console.WriteLine("Rno :" + rno);
Console.WriteLine("Total :"+total);
}
}
}
using System;
Class :
A Class is a construct that enables you to create your own custom types, by grouping variables and methods and events together.
now this becomes a type for us which can be used in or projects.
Def :
A class is a data structure that may contain data members (constants and fields), function members (methods, properties, events, indexers, operators, instance constructors, destructors and static constructors), and nested types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class.
syntax of declaring a class:
class classname
{
variables
properties
methods
events
}
Here class is a keyword which is preceded by the access levels
We have different access levels used in our programming while constructing a class. they are 1) private
2) public
3) protected
4) internal
using this access specifiers we can block the access of elements inside the class.
Private: the data declared in private section cannot be access outside the class. we can use methods declared in public section or properties declared in public section to access these private data.
Public : the data declared in public section can be accessed any where in the code in that namespace.
protected : this is similar to private but data declared under protected can be used in the class which are inherited.
internal: the type of member can be accessed by any code in the same assembly (dll)
Class is a model :
we need to create an object in order to access the data inside the class.
an object is an instance of a class.
syntax to create an object from a class:
classname objectname = new classname();
to find the reference of class click on F12 on that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstClass
{
class Program
{
static void Main(string[] args)
{
student s = new student();
s.addstudentdata();
Console.WriteLine();
s.displaystudentdata();
Console.Read();
}
}
public class student
{
private
int rno, m1, m2;
protected
int total;
public void addstudentdata()
{
Console.WriteLine("Enter the student information : rno , m1, m2 :");
rno = Convert.ToInt32(Console.ReadLine());
m1 = Convert.ToInt32(Console.ReadLine());
m2 = Convert.ToInt32(Console.ReadLine());
}
public void displaystudentdata()
{
total = m1 + m2;
Console.WriteLine("Rno :" + rno);
Console.WriteLine("Total :"+total);
}
}
}
Constructors :
Constructors are methods which are invoked automatically when an object of its type is created.
to define a constructor it has some rules.
1)Name of the constructor method should be same of class name
1)This constructor should be present in the public because, compiler would take responsibility to invoke this constructor method.
There are different types of constructors :
1) default constructor
2) parameterized constructor
3) copy constructor
4)private constructor
Default constructor would be defined in the program by default and it is not mandatory
parameterized constructor is one which has parameters in it
we will be using this keyword which refers to the current instance of the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstClass
{
class Program
{
static void Main(string[] args)
{
student s = new student(10,30);
Console.WriteLine();
s.displaystudentdata();
student D = new student(40,50);
Console.WriteLine();
D.displaystudentdata();
Console.Read();
}
}
public class student
{
private
int rno, m1, m2;
protected
int total;
public student()
{
rno = 100;// if you want to initialize the default values they should be coded here.
}
public student(int mark1, int m2)
{
m1 = mark1;
this.m2 = m2;
}
public void displaystudentdata()
{
total = m1 + m2;
Console.WriteLine("Rno :" + rno);
Console.WriteLine("Total :"+total);
}
}
}
* write a program to display the details of employees
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
//Syntax to create array of objects
employee[] e = new employee[5];
//Create memory for each object
for (int i = 0; i < 5; i++)
{
e[i] = new employee();
}
//Read data for each object
for (int i = 0; i < 5; i++)
{
e[i].ReadData();
}
//Display data of each employee
for (int i = 0; i < 5;i++ )
{
Console.WriteLine("Employee " + (i+1) + " details");
e[i].getData();
}
Console.Read();
}
}
class employee
{
private int eno;
private string eName;
public void getData()
{
Console.WriteLine("Eno :-" + eno + " Ename :-" + eName);
}
public void ReadData()
{
Console.Write("enter Eno :");
eno = Convert.ToInt32(Console.ReadLine());
Console.Write("enter Ename :");
eName = Console.ReadLine();
}
}
}
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
//Syntax to create array of objects
employee[] e = new employee[5];
//Create memory for each object
for (int i = 0; i < 5; i++)
{
e[i] = new employee();
}
//Read data for each object
for (int i = 0; i < 5; i++)
{
e[i].ReadData();
}
//Display data of each employee
for (int i = 0; i < 5;i++ )
{
Console.WriteLine("Employee " + (i+1) + " details");
e[i].getData();
}
Console.Read();
}
}
class employee
{
private int eno;
private string eName;
public void getData()
{
Console.WriteLine("Eno :-" + eno + " Ename :-" + eName);
}
public void ReadData()
{
Console.Write("enter Eno :");
eno = Convert.ToInt32(Console.ReadLine());
Console.Write("enter Ename :");
eName = Console.ReadLine();
}
}
}
how to use class in c#.Net in console application.
Reviewed by Share less to Learn More
on
7:08 PM
Rating:
No comments