Sunday, April 12, 2015

What is a class?

What is a Class?
A class  can be defined as a blueprint. A class consists of a data and behavior. When we define a class we declare a data that it contains and the code that operates on it.

The data in C# is represented by fields and the behavior is represented by methods.
In general terms, the data is contained in data members
                            the code that operates on the data is contained in the function members , The data members and function members together are called as class members

Class member definition:
A class can have Fields, properties, methods, and events  collectively referred to as class members.

The syntax for the Class is :
[access modifier] [class keyword] [class Name]
public class Customer
{
    //Fields, properties, methods and events go here...
}





Here public is the access modifier
         class keyword is used to declare that it is a class
         Customer is the name of the class

Let us create a simple Customer class

In general the customer will have first name and last name. So the First Name and Last Name can be treated as the data part of the class 
And when we give the first Name and the last Name of the customer the system should print the Full Name of the customer. Returning the Full Name can be treated as the behavior of the class.

Let us write the code for the above example



public class Customer
    {
         string _FirstName;
         string _LastName;

        public string FullName(string fName, string lName)
        {
            string name = fName + " " + lName;
            return name;
        }


    }

 The fields _FirstName and _LastName is a data of type string. The FullName is a function also called as method adds the FirstName and the Last Name and returns the fullName.