Skip to main content

Classes and Objects In C#

Class  is a collection of fields, properties and methods. It depicts real life entities and is a fundamental concept in Object Oriented Programming.
 A class is a user-defined blueprint  from which objects are created . A class is a user defined data type of reference type.

Lets take a example of a Car . It has different attributes like color,maximum speed,no of gears on with a built in audio player  and so on.

public class Car
{
    private string  _color; //a variable and a field with data type string
    // a variable and  a property  
    public string Color  
    {
        get
        {
            return _color;
        }
        set
        {
            _color= value;
        }
public void  PlayAudio()
{
//implementation logic
}
}

Declaration Of Class: 
<access specifier><keyword:class> <class name: Car>
public class car

if access identifier is not explicitly mentioned a class has internal access identifier as default while methods and properties have private access identifier.

Fields :  field is private to the class and stores the actual data.

Properties : Properties are exposed contract which uses the underlying fields.
                  Properties are also called accessors as they offer a way to get and set a field in the class.
                  Properties can be read-only,write-only or both by declaring get{},set{} or both.

Object :
Object is an instance of a class which is created at run time. It is a memory  allocation according to the class of which it is an instance of  . It is used to access properties and methods in a class.

Initializing an object
An object is initialized by using the keyword new.

Car c=new Car();
 here c is an object of type car.
if we split the above initialization in two lines
Car c ; // it is a declaration which states that c is a variable of type class c;
c=new Car();// now c is an object of class Car

and we can access the properties and methods of the class
c.Color=red;  //assigning value to the property of the object
string color=c.Color; //getting the value of the property from the object
c.PlayAudio(); //invoking method





Comments

Popular posts from this blog

Using SqlDataAdapter to fill DataTable in c#

public DataTable List(string sql)         {             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);             SqlCommand cmd = new SqlCommand(sql, con);             DataTable dt = new DataTable();             SqlDataAdapter da = new SqlDataAdapter(cmd);             try             {                              con.Open();                 da.Fill(dt);             }             finally             {                 con.Close();             }             return dt;         }

Pivot in Oracle for Dynamic Columns

Pivot in SQL refers to the change of rows into columns based on specific constraints. An example of pivot could be the transpose of matrix. Example: a matrix containing day as 1st row and sales as 2nd row for  specific day sun    10 mon  20 tue    30 The transpose of the above matrix would be sun mon tue 10  20  30 This is an example scenario of pivot in sql. Now, Lets get on to the actual tables in database and see how we can use pivot . Starting with a fictional scenario, assume a table which contains two columns DAYS and SALES. CREATE TABLE TEST.DAILY_SALES (   DAYS   VARCHAR2(10),   SALES  NUMBER ) insert into DAILY_SALES('SUN',10); insert into DAILY_SALES('MON',20); insert into DAILY_SALES('TUE',15); insert into DAILY_SALES('WED',25); insert into DAILY_SALES('THU',10); insert into DAILY_SALES('FRI',30); insert into DAILY_SALES('SAT',5); Now when we execute the following select statement se