Monday, 28 October 2013

Class in C# .Net

Class in C# .Net

Class: Class is nothing but a blue print or template. Classes are reference types and they get allocated on heap. It is user defined data type that used to contains different items like

·         Data Field
·         Member  Function
·         Property
·         Constructor
·         Delegates
·         Event
·         Attribute
·         De Constructor
·         Indexer

Class Theory: There are two type class theories 
·         Within Class
·         Outside of class

Rules  of Object Oriented Programming:
·         To access data field and member function of class we have to create object of class
·         Procedure  to create object of class
·         Every object  create separate memory block
·         Keyword this  is  the owners of the class


General Syntax to create a Class object:
<Class Name> <object name>=new <Class Name> ();

 For Example: Class1 c1=new Class1 ();
Or
  <Class Name> <object name>;
  <Object name>=new <Class Name> ();

       For Example: Class1 c1;
              c1=new Class1 (); 

Note: we can access public region with the help of object directly but we cannot access private region directly


Example With Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public class Class1
{
private int a, b;
public int c;

public void SetData()
{
a = 5;
b = 7;
c = 9;
}
}
private void button1_Click(object sender, EventArgs e)
{

Class1 ob1 = new Class1();
ob1.SetData();
ob1.c = 10;

//ob1.a=25  // we cannot initialized a ,because a is private member
//ob1.b=34;

}
}
}

  

0 comments:

Post a Comment