Sunday, 17 December 2017

Generic in c#

Generic used to code reuse, type safety and increase performance, it consists in declaring a Type with Type parameter that will be instantiate. Declare generic with the help of <T> where T is the Parameterized type. Classes, Interfaces, Methods, Properties, Delegates can be used as a generic type.

Simple program to compare two string parameters using Compare method in CompareValue class.

using System;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            CompareValue check = new CompareValue();

            Console.WriteLine(check.Compare("A", "A"));

            Console.ReadKey();
        }
    }

    public class CompareValue
    {
        public bool Compare(string value1, string value2)
        {
            return value1.Equals(value2);
        }
    }
}

Output will be:
True


if check string parameter and integer parameter pass Compare method will throw errors

  Console.WriteLine(check.Compare("A", "123"));


Generic method:


using System;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            CompareValue check = new CompareValue();

            Console.WriteLine(check.Compare<string>("A", "A"));

            Console.ReadKey();
        }
    }

    public class CompareValue
    {
        public bool Compare<T>(T value1, T value2)
        {
            return value1.Equals(value2);
        }
    }
}


Output:
True


Generic Class:


using System;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            CompareValue<string> check = new CompareValue<string>();

            Console.WriteLine(check.Compare("A", "A"));

            Console.ReadKey();
        }
    }

    public class CompareValue<T>
    {
        public bool Compare(T value1, T value2)
        {
            return value1.Equals(value2);
        }
    }
}

Output:
True


Create integer generic class


using System;

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            CompareValue<int> check = new CompareValue<int>();

            Console.WriteLine(check.Compare(100, 100));

            Console.ReadKey();
        }
    }

    public class CompareValue<T>
    {
        public bool Compare(T value1, T value2)
        {
            return value1.Equals(value2);
        }
    }
}

Output:
True


If pass parameter 100 , 200 output will be false value:

Console.WriteLine(check.Compare(100, 200));

Output will be:
False



Get method type and method name

Console.WriteLine("Enter TypeName:");
string TypeName = Console.ReadLine();

Type type = Type.GetType(TypeName);

MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
    Console.WriteLine(method.ReturnType.Name + " " + method.Name);
}


Console application shows Return type and name of method; method display more than one time its overloaded methods. Full list
Get property and constructor
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo prop in properties)
{
    Console.WriteLine(prop.PropertyType.Name + " " + prop.Name);
}


ConstructorInfo[] constructor = type.GetConstructors();
foreach (ConstructorInfo cons in constructor)
{
    Console.WriteLine(cons.ToString());
}



Complete code:
using System;
using System.Reflection;

namespace Reflection
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter TypeName:");
            string TypeName = Console.ReadLine();

            Type type = Type.GetType(TypeName);

            MethodInfo[] methods = type.GetMethods();
            foreach (MethodInfo method in methods)
            {
                Console.WriteLine(method.ReturnType.Name + " " + method.Name);
            }

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo prop in properties)
            {
                Console.WriteLine(prop.PropertyType.Name + " " + prop.Name);
            }

            ConstructorInfo[] constructor = type.GetConstructors();
            foreach (ConstructorInfo cons in constructor)
            {
                Console.WriteLine(cons.ToString());
            }

            Console.ReadKey();
        }
    }
}


Friday, 15 December 2017

Enum GetValues and GetNames Methods

Create Shapes Enum

    public enum Shapes
    {
        Circle,
        Square,
        Rectangle,
        TryAngle
    }

Create integer array then call to Shapes Enum

            //Get Shapes value
            int[] shapeValues = (int[])Enum.GetValues(typeof(Shapes));

            Console.WriteLine("Values from Shapes Enum");
            foreach (int values in shapeValues)
            {
                Console.WriteLine(values);
            }

Create string array then call to Shapes Enum
            //Get Shapes names
            Console.WriteLine("Names from Shapes Enum");
            string[] shapeNames = (string[])Enum.GetNames(typeof(Shapes));

            foreach (string Names in shapeNames)
            {
                Console.WriteLine(Names);
            }
Print both Shapes and values
            //Get Shape names & Values
            Console.WriteLine("Names & values from ShapesValue Enum");
            foreach (ShapesValue shapes in Enum.GetValues(typeof(ShapesValue)))
            {
                Console.WriteLine("{0} - {1}", shapes, (int)shapes);
            }





Generic in c#

Generic used to code reuse, type safety and increase performance, it consists in declaring a Type with Type parameter that will be instant...