Tuesday, December 27, 2011

Basic C#

Question: What is the difference between Class and Structure (Struct)?
Ans: 1) structure: - In structure have a by default public. 
                       In class have a by default private.
       2) Structure cannot be inherited.
          But class can be Inherit.
       3) There is no data hiding features comes with structures. 
          Classes do, private, protected and public.
       4) A structure can't be abstract, 
          A class can be abstract.
       5) A structure is a value type, 
          While a class is a reference type.
       6) A structure is contain only data member,
          But class contains data member and member function.
       7) In a Structure we can't initiates the value to the variable
          But in class variable we assign the values.
       8) Structure are value type, they are stored as a stack on memory.  Whereas class is reference type, they are stored as heap on memory.
Question: Difference between abstract class and interface. 
§  Abstract class defines few or none of the methods, but interface defines all the methods.
§  Abstract classes should have subclasses else that will be useless, but Interfaces must have         implementations by other classes else that will be useless.
§  Only an interface can extend another interface, but any class can extend an abstract class.
§  All variable in interfaces are final by default.
·         Accessibility modifier (Public/Private/internal) is allowed for abstract class, but Interface   doesn’t allow accessibility modifier
·         Class can inherit only one abstract class can implement more than one interface.
·         Cannot create instance of abstract class.

Question: What is the Difference between Overriding and overloading?
Ans:         Overloading-------
·     Having same method name with different signatures.
·     Overloading does not take return type to differentiate overloaded method.
·     Method can be different  access modifier(public, private, protected) 
Int add (int a, int b)
Int add (float a, float b)
                                         Are overloaded methods
§  Methods name and signatures must be same.
§  Overriding comes with inheritance concept here parent and child both contains the same method. 
§  When execution takes place; child's method is called.
§  It use for implementing the Inheritance concept.
§  Redefine the base class method in drive class; method name and signature both are same.
§  Method must be public.

        Question: what is the difference between value types and reference types?
                      Value Type…….
§  Value type - bool, byte, decimal, double, enum, float, int, long, short, strut, uint, ulong
§  Value types are stored in the Stack
§  Value types directly contain their data, and instances of value types are either         allocated on the stack or allocated inline in a structure. 
§  Value types can be built-in (implemented by the runtime), user-defined, or enumerations. 
                        Reference Type…….                               
§  Class, delegate, interface, object & string are reference type.
§  Reference types are stored in the Heap.
§  Reference types store a reference to the value's memory address, and allocated on the heap. 
§  Reference types can be self-describing types, pointer types, or interface types. 
§  The type of a reference type can be determined from values of self-describing types.
§  Self-describing types are further split into arrays and class types. The class types are user-defined classes, boxed value types, and delegates.

No comments: