Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them. For more information, see Attributes.
Here's a simple example of Reflection using the static method GetType - inherited by all types from the Object base class - to obtain the type of a variable:
// Using GetType to obtain type information:
int i = 42;
System.Type type = i.GetType();
System.Console.WriteLine(type);
The output is:System.Int32
In this example, Reflection is used to obtain the full name of a loaded assembly:// Using Reflection to get information from an Assembly: System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll"); System.Console.WriteLine(o.GetName());
The output is:
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
************************************************************************************************************Method Overloading in C#.NETThe process of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading.In VB.net when you are overloading a method of the base class in derived class, then you must use the keyword “Overloads”.But in C# no need to use any keyword while overloading a method either in same class or in derived class. While overloading methods, a rule to follow is the overloaded methods must differ either in number of arguments they take or the data type of at least one argument.Example for Method Overloadingusing System; namespace ProgramCall { class Class1 { public int Sum(int A, int B) { return A + B; } public float Sum(int A, float B) { return A + B; } } class Class2 : Class1 { public int Sum(int A, int B, int C) { return A + B + C; } } class MainClass { static void Main() { Class2 obj = new Class2(); Console.WriteLine(obj.Sum(10, 20)); Console.WriteLine(obj.Sum(10, 15.70f)); Console.WriteLine(obj.Sum(10, 20, 30)); Console.Read(); } } }Output30 25.7 60Note Method overloading provides more than one form for a method. Hence it is an example for polymorphism.In case of method overloading, compiler identifies which overloaded method to execute based on number of arguments and their data types during compilation it self. Hence method overloading is an example for compile time polymorphism.*****************************************************************Method Overriding in C#.NETCreating a method in derived class with same signature as a method in base class is called as method overriding.Same signature means methods must have same name, same number of arguments and same type of arguments. Method overriding is possible only in derived classes, but not within the same class. When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.Examples for Method Overriding in C#using System; namespace methodoverriding { class BaseClass { public virtual string YourCity() { return "New York"; } } class DerivedClass : BaseClass { public override string YourCity() { return "London"; } } class Program { static void Main(string[] args) { DerivedClass obj = new DerivedClass(); string city = obj.YourCity(); Console.WriteLine(city); Console.Read(); } } }Output London Example - 2 implementing abstract methodusing System; namespace methodoverridingexample { abstract class BaseClass { public abstract string YourCity(); } class DerivedClass : BaseClass { public override string YourCity() //It is mandatory to implement absract method { return "London"; } private int sum(int a, int b) { return a + b; } } class Program { static void Main(string[] args) { DerivedClass obj = new DerivedClass(); string city = obj.YourCity(); Console.WriteLine(city); Console.Read(); } } }Output London*****************************************************************Inheritance in C#Creating a new class from existing class is called as inheritance.Output First Number: 30 Second Number: 40 Third Number: 50 Sum : 70
//All the car properties can be used by the supercarWhen a new class needs same members as an existing class, then instead of creating those members again in new class, the new class can be created from existing class, which is called as inheritance. Main advantage of inheritance is reusability of the code. During inheritance, the class that is inherited is called as base class and the class that does the inheritance is called as derived class and every non private member in base class will become the member of derived class. If you want the base class members to be accessed by derived class only , you can apply access modifier Protected to the base class member. There are 5 Different types of inheritance. Syntax
class SuperCar : car
{
}
[Access Modifier] class ClassName : baseclassname{}C# Inheritance Example
//Example program demonstrates singular inheritance
using System;
namespace ProgramCall
{
class BaseClass
{
//Method to find sum of give 2 numbers
public int FindSum(int x, int y)
{
return (x + y);
}
//method to print given 2 numbers
//When declared protected , can be accessed only from inside the derived class
//cannot access with the instance of derived class
protected void Print(int x, int y)
{
Console.WriteLine("First Number: " + x);
Console.WriteLine("Second Number: " + y);
}
}
class Derivedclass : BaseClass
{
public void Print3numbers(int x, int y, int z)
{
Print(x, y); //We can directly call baseclass members
Console.WriteLine("Third Number: " + z);
}
}
class MainClass
{
static void Main(string[] args)
{
//Create instance for derived class, so that base class members // can also be accessed
//This is possible because derivedclass is inheriting base class
Derivedclass instance = new Derivedclass();
instance.Print3numbers(30, 40, 50); //Derived class internally calls base class method.
int sum = instance.FindSum(30, 40); //calling base class method with derived class instance
Console.WriteLine("Sum : " + sum);
Console.Read();
}
}
}***********************************************************What are Value Types and Reference Types in .Net
***************************************************************************According to MSDN, A data type is a value type if it holds the data within its own memory allocation. A reference type contains a pointer to another memory location that holds the data. All the data types in .net are classified in to value types and reference types.
- The data types whose values are directly stored in stack memory area are called as value types and the data types whose values are stored in heap memory area and its address is stored in a variable in stack memory area are called as reference types.
- Among all built in data types of .net string and object are reference type and all other data types are value types.
Value Types---------------- Value types include the following:
- Among user defined data types, class, interface, delegate and arrays are reference type while structure and enumeration are value type.
Reference Types ----------------------Reference types include the following:
- All numeric data types
- Boolean, Char, and Date
- All structures, even if their members are reference types
- Enumerations, since their underlying type is always SByte, Short, Integer, Long, Byte, UShort, UInteger, or ULong
- String
- All arrays, even if their elements are value types
- Class types
- Delegates
No comments:
Post a Comment