Monday, May 13, 2013

What is the difference between constant and readonly?


Constant
  • Its value is determine at compilation time and embedded in the IL.
  • Set at compilation time.
  • Can only for define for primitive type.
ReadOnly
  • Its value is initialized at the run-time inside the constructor.
  • Set during run-time.
  • Can be define for value type and reference type.

Monday, May 6, 2013

Difference between reference type and value type

Value Type
  • Allocated on stack
  • Have both representation i.e. boxed form & unboxed form
  • Derived from System.ValueType
  • No Method can be abstract
  • While assigning field by field copy is made.
  • Storage is free as soon as they goes out of scope

Reference Type
  • Allocated on heap
  • Always has boxed formed
  • Derived from any object other than System.ValueTye
  • Methods can be abstract
  • While assigning only memory address is copied
  • Storage is freed using Garbage collection

Monday, April 29, 2013

Casting and Type Conversions in C#


Casting or type conversion is a method by which a data is converted from one data type to another. There are 2 types of type casting.

Implicit type casting.
Implicit casts are the casts that do not require any special syntax and it’s done by complier by its own. There is not data loss in implicit conversion. However there are some rules by which are followed by complier for performing the casting.

a. Derived class to base class.

If we try to assign a value from derived class to a base class it will work. That is because a derived class always is-a base class. Also, from a memory perspective,  a derive class object contain the base class object in it and it can be access by a base class object without any problem. For example following code work without any problem..

class Base
{

}
class Derived : Base

}
class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        Base b = d;     // object b can access base class part of object d
    }
}

b. Primitive types.
Since the complier has intimate knowledge about the primitive types it’s apply some special rules of type casting. The things here to remember is it allows the narrow type to be assign to wider type. Since this there is no chance to data loss this is consider as safe typing and hence no cast operator is expected from programmer.

static void TestCasting()
{
    int i = 10;
    float f = 0;
    f = i;  // An implicit conversion, no data will be lost.
 
}


Explicit type casting.
In explicit type casting there is possibilities type data loss hence it is essential for programmer to mention this to compiler by special syntax.

a. Base class to derive class.
C# compiler require the programmer to explicitly cast an object of a base type to an object of its derived type as there is possibility of failure at run time.

Base b = new Base();
Derived d = (Derived)b;

b. Primitive types.
Programmer also needs to explicitly mention the cast in case he needs to cast a wider type to narrow type. The reason here is these type of conversion might result in data loss and hence it unsafe.

static void TestCasting()
{
    int i = 0;
    float f = 0.5F;
    i = (int)f;  // An explicit conversion. Information will be lost.
}

Thursday, April 4, 2013

Sunil Narine is still a mystery

Narine started the IPL 6 from where he left last session. Still Delhi’s batsman unable to read his bowl. His ball was turning both ways. Except Jayawardene all batsman felt clueless in front of Narine. Surely they missed Shewag who was the one who had shown some success against him last session. Overseas batsman was looked more pretty in front of him.

Also Gautam use Narine very smartly as a attacking bowler and utilized other slow bowlers like Bhatiya in between small spells of Narine. Narine got the good support from Breet Lee and Balaji.

If other team wanted to beat Kolkatta, they need to find out a way to play Narine.

Sunday, March 31, 2013

Highlights of IPL 2013

A huge and fantastic victory over Australia sets the atmosphere for IPL. After a long and very bad losses India shown some good cricket in favorite condition. Along with more cricket and excited finishes there are plenty of highlight of this session of IPL.
  • Cricketing future of senior cricketers like Virender Sehwag and Gaoutam Gambhir are much dependent on this IPL session.
  • Bhuvneshwar Kumar shown very promise in domestic session. I am very excited to see him in condition which favors pace balling and have quality batsman especially against swing balling.
  • India played with 3 spinners in India but in SA it is most likely that only best spinner will be picked. This indicates a battle among inform spinners for capturing the place in playing 11 in SA.
  • India U-19 team just has won the world cup. It would be very excitement to see these player again world class player and who among them will capture the place in senior team.