Search This Blog

Monday, 29 February 2016

Indexer in c#

Indexers allow class to be used just like an array.

Q. How to create an Indexers?
A. In place of Method name use "this" keyword and in place of () Bracket use [] Square Bracket.

Normal Method = 
public string MethodName(int index)
Indexers      = public string this[int index]

Indexers have get and set accessor.

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

namespace IndexerClass
{
    class IndexerClass
    {
        // Array of string values
        private string[] week = { "Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday" };

        // Return Array Length
        public int Length
        {
            get
            {
                return week.Length;
            }
        }

        // Indexer declaration
        public string this[int index]
        {
            get
            {
                return week[index];
            }

            set
            {
                week[index] = value;
            }
        }

        // Indexer declaration
        public int this[string name]
        {
            get
            {
                int index = 0;

                while (index < Length)
                {
                    if (week[index] == name)
                    {
                        return index;
                    }
                    index++;
                }
                return index;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IndexerClass p = new IndexerClass();

            // Use the indexer's get accessor to return value
            for (int i = 0; i < p.Length; i++)
            {
                Console.WriteLine(p[i]);
            }

            // Use the indexer's get accessor to return index
            Console.WriteLine(p["Thursday"]);

            Console.ReadKey();
        }
    }
}

OUTPUT

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
4

Note:-

For More Info visit Microsoft.com

Friday, 26 February 2016

Check MVC Version

Replace your HomeController Index method with following code

public string Index()
{
     return typeof(Controller).Assembly.GetName().Version.ToString();
}

Using Reflection

typeof(Controller).Assembly.GetName().Version.ToString()

Using Project References folder


Expand References folder => Search System.Web.Mvc => Right Click on it => Click on Properties => Property details window open => Look at the Version property

Wednesday, 24 February 2016

Extension method in C#

Explanation
  1. Extension method must be defined in a non-generic static class.
  2. Extension method must be static.
  3. Extension method first parameter must be start with "this" keyword.
  4. Only one "this" keyword place in Extension method parameter list.
Complete Code

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


namespace ExtensionMethods
{
    class InstanceClass
    {
        public static bool IsNumeric(string value)
        {
            float output;

            return float.TryParse(value, out output); ;
        }

        public static string CamelCaseLetter(string value)
        {
            StringBuilder sb = new StringBuilder();

            if (value.Length > 0)
            {
                string[] strSplit = value.Split(' ');

                foreach (var str in strSplit)
                {
                    char[] charArray = str.ToCharArray();

                    charArray[0] = char.ToUpper(charArray[0]);

                    sb.Append(new string(charArray) + " ");
                }

                return sb.ToString().TrimEnd(' ');
            }
            return value;
        }
    }

    static class StaticClass
    {
        public static bool IsNumeric(this string value)
        {
            float output;

            return float.TryParse(value, out output); ;
        }

        public static string CamelCaseLetter(this string value)
        {
            StringBuilder sb = new StringBuilder();

            if (value.Length > 0)
            {
                string[] strSplit = value.Split(' ');

                foreach (var str in strSplit)
                {
                    char[] charArray = str.ToCharArray();

                    charArray[0] = char.ToUpper(charArray[0]);

                    sb.Append(new string(charArray) + " ");
                }

                return sb.ToString().TrimEnd(' ');
            }
            return value;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string strValue = "hello world";
//Calling Instance Method
            Console.WriteLine(InstanceClass.IsNumeric(strValue));

//Calling Extension Method
            Console.WriteLine(strValue.IsNumeric());

//Calling Instance Method
            Console.WriteLine(InstanceClass.CamelCaseLetter(strValue));

//Calling Extension Method
            Console.WriteLine(strValue.CamelCaseLetter());

            Console.ReadKey();
        }
    }
}


OUTPUT


Note:-

There is no big difference between instance method and extension method. Only “this” keyword of method parameter differentiating with the instance and extension method and the rest of the code as same.

Focus on highlighted code.

How to Call?

Instance method = ClassName.MethodName(value)

Extension method = value.MethodName()