Showing posts with label C# Basics. Show all posts
Showing posts with label C# Basics. Show all posts

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.




Sunday, February 22, 2015

C# Basics

Namespace: A Namespace is used to organize your code. It is collection of classes, interfaces, structs, enums and delegates
DataTypes: C# is a strongly-typed language. Before a value can be stored in a variable, the type of the variable must be specified .
The Built in datatypes in C#
  • Boolean type
  • Integral Type
  • Floating Type
  • Decimal Type
  • String Type
Boolean Type: A variable of Boolean type will have values only True or False. The keyword bool is used in defining the datatype
eg: bool X = true;
The Variable X is of type bool and has the value true. If you try to assign any other value to the bool variable X then it will give a compile error

Integral Type: The integral datatypes are used to store  numbers. There are many Integral datatypes 
  •  sbyte 
  • byte
  • short
  • ushort
  • int
  • uint
  • long
  • ulong
  • char
C# datatypes are categorized into two categories
       Value  Datatypes
       Reference Datatypes