Friday, April 23, 2010

Overriding – ToString () – A Simplest approach to understand it

In C# Overriding can be achieved by use of the "override" keyword. To override a method means to replace it with a new way of handling data; I mean replacing the default behavior or extending it.

Here's an example of what I mean:
public class SimpleCar
 {
      int vwidth, vheight; //Private Data Member
      public int cWidth {  set { vwidth = value; } get { return vwidth; } }
      public int cHeight { set { vheight = value; } get { return vheight; } }
      public int cArea {  get { return (vwidth * vheight); }}
     //Not Overrided built-in ToString() method
 }
Moving forward with overriding,We will take help of “ToString()” method.As we know ToString is a built-in method that returns the name of the type of the object(in case of custom type).

Have a look at the Main() method which is used to call SimpleCar-
   namespace ConsoleApplication1 {
      class Program {
            static void Main(string[] args) {
                    SimpleCar mySimpleCar1 = new SimpleCar();
                    mySimpleCar1.cWidth = 13;
                    mySimpleCar1.cHeight = 15;

                   Console.WriteLine("The area occupied by {0}, width={1} and height={2} is: 
                   {3}.", mySimpleCar1, mySimpleCar1.cWidth, mySimpleCar1.cHeight,  mySimpleCar1.cArea);

                   Console.ReadLine(); }
       }
   }

Exact output will be:
The area occupied by ConsoleApplication1.SimpleCar, width=13 and height=15 is: 195.

*Here ‘ConsoleApplication1.SimpleCar’ name comes through the help of ToString() method, As I explained earlier. Now we will simply override built-in ToString() method in our code and then we’ll see what we get.

public class SimpleCar {
       int vwidth, vheight; //Private Data Member
       public int cWidth { set { vwidth = value; } get { return vwidth; } }
       public int cHeight { set { vheight = value; } get { return vheight; } }
       public int cArea { get { return (vwidth * vheight); } }

       //Overrided built-in ToString() method
       //Here I have provided an alias for the class(-type-)
       public override string ToString()  { return "Overrided SimpleCar"; }
}

The only change between this method and any other method is the use of the ‘override’ keyword. It is as simple as earlier. Now when the same Main() method is executed, the output will be:

The area occupied by Overrided SimpleCar, width=13 and height=15 is: 195.

I hope you get the reason, How ‘Overrided SimpleCar’ came into the picture.By overriding – I mean we have replaced the default behavior of built-in ToString () method.
-As of now we get bit understanding of overriding; now we need to delve into overloading and overriding together.
-
*Hasbro Power Tour Electric Guitar (Black)

Thursday, April 22, 2010

Abstraction vs Encapsulation


Abstraction is the process of taking a concept, identifying the attributes and behaviour and being able to turn that concept into code(Class) such that you will get an abstract view of the concept while Encapsulation is the process of organizing the required data types, methods to access them and defines a way to communicate through outer world, which mean's to get the job done.

In Object oriented programming abstraction is extremely important as by working more abstractly we can solve more and more difficult problems because we don’t have to concentrate on the lower level details.
  • “In another term we can say Abstraction comes first, Encapsulation satisfies what abstraction promise to do so.”
-Abstraction ask's to concentrate on “What” system does and encapsulation states “How” system does.


Elaborating topic more, For Abstraction we can say if we are not willing to know the cumbersome detailed information (because it might be too complex...) and willing to consider only meaningful data! Which help’s to "simplify" complex task and get clear arrangement/management.

While encapsulation shakes hands with if we didn’t want to let others access some important data and hence we put them within class and declare using access specifier. As a result, only the member functions can access them.

-Here I left one topic untouched i.e. information hiding, I will try to relate information hiding with this topic in next post.
-
*ASUS UL30A-X5 Thin and Light 13.3-Inch Black Laptop (12 Hours of Battery Life)

Wednesday, April 21, 2010

Defining Nullable Types in C#

Explaining it from very basic that a nullable data type is the one that can contains the defined data type or the value of null.Defining a nullable type is basically similar to defining the equivalent non-nullable type having single difference is in the use of the ? Type modifier.

As to define an integer, simply this declaration will be fine:
int simpleInt = 1;

But to make it able to store null value or to become a nullable datatype,we need to declare it as:
int? simpleNullableInt = 1;

The nullable version is actually a structure that consists of a value type and with a flag to indicate whether the value is null. Being more specific, a nullable type has two publicly readable properties, HasValue and value. HasValue is simply a bool variable that is true if there is a value stored otherwise, it is false if the variable is null. If HasValue is true, you can get the value of the variable. If it is false and you attempt to get the value definetly an exception will be thrown. null is now become a keyword for C#. Adding more words to it, it can be assigned to a nullable variable. The following are two valid assignments for a nullable variable:
double? simpleDouble = 3.14159;
double? simpleOtherDouble = null;


As simpleDouble is assigned a value, but could also be assigned null and in the second statement, simpleOtherDouble is initialized to contain a null value—something you can't do with a non-nullable type. A nullable type can be used in the same way that a regular value type can be used. But be sure that it doesn't have null value. Implicit conversions are built in for conversion between a nullable and non-nullable variable of the same type. It means we can assign an integer to a nullable integer and vice versa.

-
I know its a bit story about nullable datatype, But don't worry in future I will add more details to this story..
-
*The Big Short: Inside the Doomsday Machine