Class:
Class is a collection of members.It contain fields, properties, methods, operators, constructors, destructors, events and indexes.
Syntax for Class:
<Access modifier> class <ClassName>
{
//Member(s)
}
Access Modifiers:
Access modifiers/Access specifiers are keywords which are specifying or restricting access level or accessibility of a class or class members.
In C#.NET access modifiers are of five types
- Public
- Private
- Protected
- Internal
- Protected internal
Object is a instance of a class.which is used to access the class members.
Syntax for Object Creation:
<ClassName> <object name>=new <ClassName>;
Syntax for accessing a class member with the respective of object:
<Object name>.<Class members>
Example program to create a class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassExamples
{
class Student
{
public int sid, smarks;
public string sname;
public void Sdata()
{
Console.WriteLine("Enter Student Details ...");
this.sid = Convert.ToInt32(Console.ReadLine());
this.sname = Console.ReadLine();
this.smarks = Convert.ToInt32(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("Student id : " + this.sid);
Console.WriteLine("Student name : " + this.sname);
Console.WriteLine("Student marks : " + this.smarks);
}
}
class StudentMain
{
static void Main(string[] args)
{
Student s = new Student();
s.Sdata();
s.Display();
Console.Read();
}
}
}
No comments:
Post a Comment