Reflection in C#

Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. it allows code to inspect other code within the same system. The main class for reflection is the System. Type class, which is an abstract class representing a type in the Common Type System (CTS). When you use this class, you can find the types used in a module and namespace and also determine if a given type is a reference or value type.


Benefits of Reflection C#


1.      Use Module to get all global and non-global methods defined in the module.
2.      Use Assembly to load modules listed in the assembly manifest.
3.      Use PropertyInfo to get the declaring type, reflected type, data type, name and writable status of a property or to get and set property values.
4.      Use MethodInfo to look at information such as parameters, name, return type, access modifiers, and implementation details.
5.      Use EventInfo to find out the event-handler data type, the name, declaring type and custom attributes.
6.      Use ConstructorInfo to get data on the parameters, access modifiers, and implementation details of a constructor.




Constructors in C#


The constructor is special methods which get called automatically when an object of a class is created. Constructors are specially used to initialize data members.

Types of constructors:-
1.     Default Constructor
2.     Parameterized Constructor
3.     Copy Constructor
4.     Static Constructor


   public class SampleClassNitin

    {

        public SampleClassNitin()

        {

            Console.WriteLine("SampleClassNitin  Test Method");

        }
    }

Default Constructor
A constructor without having any parameters called the default constructor.

Parameterized Constructors
A constructor with at least one parameter is called as parameterized constructor.

Constructor Overloading
When we create another constructor with the same method name with different parameters called Constructor Overloading.

Copy Constructor
A parameterized constructor that contains a parameter of the same class type is called as copy constructor.


Static Constructor

If we declared constructor as static and it will be invoked only once that type of constructor is called Static Constructor.


Try-Catch-Finally in c#

Exceptions allow an application to transfer control from one part of the code to another. When an exception is thrown, the current flow of the code is interrupted and handed back to a parent try-catch block. C# exception handling is done with the following keywords: try, catch, finally, and throw

Syntax



try
        {
            //statements that may raise exceptions
        }
        catch
        {
            //Catch the exception
        }
        finally
        {
            //This block of code will always get executed whether an exception occurs or not as long                as there are no exceptions within this block itself.
        }




Example 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class ExceptionHandlingExampleProgram
    {
        static void Main(string[] args)
        {
            object o2 = null;
            try
            {
                int i2 = (int)o2;   // Error 
            }
            catch (Exception exceptionObj)
            {
                // log exception from exceptionObj
            }
            finally
            {

                // do cleanup work here
            }

        }
    }
}