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

Connection String in ASP.NET With SQL Server

Connection string can be placed in web.config file found in root directory of the application from .NET 3.5 onward. connection string can be specified with an xml tag  <connectionStrings>  inside  <configuration>  section of web.config file. < connectionStrings > < add name = "myConnectionString" connectionString = "Data Source=databaseServerName; database=database-name; uid=sqlUserName;password=sqlPassword; Integrated Security =True|false|SSPI (any one options) ; " providerName = " System.Data.SqlClient System.Data.SqlClient" /> </ connectionStrings > We can use local database server of SQL Server by using Data Source= (LocalDb)\MSSQLLocalDB we can attach a local database file to the app_data directory by using the property AttachDBFilename=|DataDirectory|\appDatabaseName.mdf in the connection string Connection String can be

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;         }