C# Classes are reference types; they are allocated on the Managed Heap. The Managed Heap is an area of memory that is managed by the Common Language Runtime, which has the ability to free unused memory blocks (objects) in a process known as Garbage Collection.
Access Modifier keywords: public, private, protected and internal.
Class members: fields, properties, methods, constructors, destructor, indexers, constants, events, delegates and operators.
Struct: Similar to Class, A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. By default it is sealed. There is no inheritance for structs as there is for classes. The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color.
class Program
{
struct Simple
{ public int Position;
public bool Exists;
public double LastValue;
};
static void Main ()
{
Simple s;
s.Position = 1;
s.Exists = false;
s.LastValue = 5.5;
}
}
Static class: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Ex: UtilityClass.MethodA ();
* Contains only static members. * Cannot be instantiated. * Is sealed. * Cannot contain Instance Constructors.
Static member: A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter. We can’t use this keyword to access Static Members.
Abstract Class: -cannot be instantiated. - An abstract class is only to be sub-classedinherit only one abstract class, but may implement multiple numbers of Interfaces. - Abstract class methods may OR may not have an implementation.
An Interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
Ex:
Interface ISampleInterface
{ Void SampleMethod (); }
Class ImplementationClass: ISampleInterface
{ // Explicit interface member implementation:
Void ISampleInterface.SampleMethod ()
{ // Method implementation. }
Static void Main ()
{ ISampleInterface obj = new ImplementationClass ();
Obj. SampleMethod (); }
}
Abstract and Virtual Method:
When a base class declares a method as virtual, a derived class can override the method with its own implementation.
If a base class declares a member as abstract, that method must be overridden in any non-abstract class that directly inherits from that class.
Sealed class: A sealed class cannot be used as a base class; it cannot also be an abstract class. By creating an instance we can use the methods in the Sealed Class.
Delegates: Delegates are used to pass methods as arguments to other methods. It allow programmer to encapsulate a reference to a method inside a delegate object.
public delegate void SimpleDeleg();
class DelegateExample
{
public void SimpMet()
{
string s= "I am From Simple Method.";
}
public static void main()
{
SimpleDeleg del = new SimpleDeleg(SimpMet);
SimpleDeleg();
}
}
Value type variables directly contain their values, which mean that the memory is allocated inline in whatever context the variable is declared.
There are two categories of value types: struct and enum.
A type that is defined as a class, delegate, array, or interface is a Reference type. At run time, when you declare a variable of a reference type, the variable contains the value null until you explicitly create an instance of the object by using the new operator
A Generic type can be declared with one or more type parameters. Generic collection classes are called strongly-typed collections because the compiler knows the specific type of the collection's elements and can raise an error at compile-time if, for example, you try to add an integer to the strings object.
Namespaces: .NET Framework uses namespaces to organize its many classes. Namespaces are used to logically arrange classes, structs, interfaces, enum and delegates. One namespace can contain other namespaces also.
The enum keyword is used to declare an enumeration (a numbered list), a distinct type consisting of a set of named constants called the enumerator list.
enum Days {low, medium, high};
class EnumSwitch
{
Public Static Void Main ()
{
Volume myVolume = Volume. Medium;
switch (myVolume)
{
case Volume. Low:
Console.WriteLine("The volume has been turned Down.");
break;
case Volume. Medium:
Console.WriteLine ("The volume is in the middle.");
break;
Case Volume. High:
Console.WriteLine ("The volume has been turned up.");
break;
}
Console.ReadLine ();
}
}
Dictionary: A Dictionary (TKey, TValue) contains a collection of key/value pairs. Its Add method takes two parameters, one for the key and one for the value.
Dictionary
{
{111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},
{112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}},
{113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}}
};
Boxing: Conversion of Value Types to Reference Types.
Ex: int32 x=10;
Object obj=x; //Implicit Boxing; No need to tell the compiler about boxing
Object obj= (Object) x; //Explicit Boxing;
Unboxing: Conversion of Reference Types to Value Types.
X= obj; //Implicit Unboxing
Constructors: Constructors are class methods that are executed when an object of a given type is created. Constructors have the same name as the class. They do not have return types, not even void. Constructor is invoked by the new operator immediately after memory is allocated for the new object.
A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated using the new operator and no arguments are provided to new.
Constructors for struct types are similar to class constructors, but structs cannot contain an explicit default constructor because one is provided automatically by the compiler.
Note: There is always at least one constructor in every class. If you do not write a constructor, C# automatically provides one for you, this is called default constructor.
Destructor: A destructor is just opposite to constructor. It has same as the class name, but with prefix ~ (tilde). They do not have return types, not even void and therefore they cannot return values. Destructor is invoked whenever an object is about to be garbage collected
Finalize () Method of Object class
Each class in C# is automatically (implicitly) inherited from the Object class which contains a method Finalize (). This method is guaranteed to be called when your object is garbage collected (removed from memory). You can override this method and put here code for freeing resources that you reserved when using the object.
Method Overloading: Method with same name but with different arguments is called method overloading. Method Overloading forms compile-time polymorphism.
Class A1
{
Void hello ()
{Console.WriteLine (“Hello”) ;}
Void hello (string s)
{Console.WriteLine (“Hello {0}”, s) ;}
}
Method Overriding: Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass. Method overriding forms Run-time polymorphism.
Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.
Class parent
{ Virtual void hello ()
{ Console.WriteLine (“Hello from Parent”) ;}
}
Class child: parent
{ Override void hello ()
{ Console.WriteLine (“Hello from Child”) ;}
}
Static void main ()
{ Parent objParent = new child ();
objParent.hello ();
}
//Output
Hello from Child.
OOPS:
Polymorphism (Many Shapes): Polymorphism is briefly described as "one interface, many implementations. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts.
There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions.
Function Overloading: In case of compile time it is called function overloading. Two or more functions can have same name: different parameters or their data types. Compiler will select the right function depending on the type of parameters passed.
Operator Overloading: Operators can be overloaded in order to perform special functions with respect to the class. With the help of operator overloading standard operations such as +, - , *, etc can be applied on the objects of the class.
Encapsulation is the procedure of covering up of data and functions into a single unit. Ex: Delegate.
Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Inheritance is the mechanism which allows a class A to inherit properties of a class B.
Note: A sealed class cannot be inherited. A sealed class is used primarily when the class contains static members. Note: Struct is implicitly sealed; so they cannot be inherited.
No comments:
Post a Comment