Sunday, February 24, 2019

What is Indexer

An Indexer is a special type of property that allows a class or structure to be accessed the same way as array for its internal collection. It is same as property except that it defined with this keyword with square bracket and parameters.



Example:

class StringDataStore
{
   
    private string[] strArr = new string[10]; // internal data storage

    public string this[int index]
    {
        get
        {
            if (index < 0 &&  index >= strArr.Length)
                throw new IndexOutOfRangeException("Cannot store more than 10 objects");

                return strArr[index];
        }

        set
        {
            if (index < 0 ||  index >= strArr.Length)
                throw new IndexOutOfRangeException("Cannot store more than 10 objects");

            strArr[index] = value;
        }
    }
}


Thursday, February 21, 2019

What is Anonymous Method

anonymous method is a method without a name. Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.


Example:

 public delegate int DelegateResultNum(int p);
        static int num = 100;
        delegate int Getnummber(int x, int Y);


        public static int Add(int x)
        {
            num += x;
            return num;
        }

        public static int multiple(int y)
        {
            num *= y;
            return num;
        }

        public static int getnum()
        {
            return num;
        }

        static void Main(string[] args)
        {
            DelegateResultNum num1 = new DelegateResultNum(Add);

            //Anonymous Method
            Getnummber d1 = delegate (int r, int z) { return r * z; };
            d1(4,5);
       
            num1(12); ;
            int x = getnum();

            Console.WriteLine(x);



        }

what is CLR,CTS,CLS

The Common Language Runtime (CLR) is the programming (Virtual Machine component) that manages the execution of programs written in any language that uses the .NET Framework, for example C#, VB.Net, F# and so on.


Function of CLR.


  • Convert code into CLI.
  • Exception handling
  • Type safety
  • Memory management (using the Garbage Collector)
  • Security
  • Improved performance
  • Language independency
  • Platform independency
  • Architecture independency.

Component of CLR
  • Class Loader

    Used to load all classes at run time.
     
  • MSIL to Native code

    The Just In Time (JTI) compiler will convert MSIL code into native code.
     
  • Code Manager

    It manages the code at run time.
     
  • Garbage Collector

    It manages the memory. Collect all unused objects and deallocate them to reduce memory.
     
  • Thread Support

    It supports multithreading of our application.
     
  • Exception Handler

    It handles exceptions at run time.


What is CTS and CLS:

C# has int Data Type and VB.Net has Integer Data Type. Hence a variable declared as int in C# or Integer in vb.net, finally after compilation, uses the same structure Int32 from CTS.

Note:

All the structures and classes available in CTS are common for all .NET Languages and the purpose of these is to support language independence in .NET. Hence it is called CTS.



CLS(Common language specification):

CLS stands for Common Language Specification and it is a subset of CTS. It defines a set of rules and restrictions that every language must follow which runs under .NET framework.