Search This Blog

Thursday, October 17, 2013

Class and object C#

C# > OOP > Class and object


A class is an abstraction of the characteristics and behavior of an object type. Classes can inherit from one other class, but can implement multiple interfaces.
A class is a template for defining objects.
A class should not be confused with an object.
An object is an instance of a class.




Difference between class and object

Class is like a template or blueprint of the object and resides on hard disk.
Object is real world implementation of a class and resides on RAM.

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Person // define Person class
    {
        public string Name { get; set; } // Name property
    }

    public partial class Form1 : Form
    {
        private void Form1_Load(object sender, EventArgs e)
        {
            Person p1 = new Person(); // create new Person object
            p1.Name = "Dan";
        }
        public Form1()
        {
            InitializeComponent();
        }
     }
}