Thursday, 8 March 2012

How to Use Extension Methods of Predication types?


Yes,In .net Framework we will see some of the extension methods using now

Ex1:
 class BasicDemo
    {
        static void Main()
        {
            int[] intarr = new int[4];
            intarr[0] = 15;
            intarr[1] = 22;
            intarr[2] = 89;
            intarr[3] = 104;

            int i=intarr.ElementAt<int>(1);
            Console.WriteLine(i);          //---22
            int value = intarr.ElementAtOrDefault<int>(2);
            Console.WriteLine(value);      //89

            int maxvalue1 = intarr.Max<int>();
            Console.WriteLine(maxvalue1);  // 104          
            Console.Read();       
        }
    }
Ex2:

class ExtensionMethod
    {
        static void Main()
        {
            int[] intarr = new int[5];
            intarr[0] = 15;
            intarr[1] = 80;
            intarr[2] = 25;
            intarr[3] = 30;
            intarr[4] = 14;

            Array.Sort<int>(intarr);   // Extinsion method
            Console.WriteLine(intarr);
            foreach (int item in intarr)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("-----------");        
            int i = intarr.First<int>();
            Console.WriteLine(i);
  Console.WriteLine("-----preparing precate to our expression-------");

        string[] str = new string[] { "anand", "balu", "chran", "dinesh" };
  Func<string, bool> abac = x => x.Length > 4;
// 1st parameter is type,    
// 2nd parameter returns Source[result]
      // abac is predicate of Fun<string,bool> type.

       string strr=str.First<string>(abac);  // HERE abac is predicate.           
            Console.WriteLine(strr);                
            int[] intarem = new int[4] { 5, 6, 7, 8 };
            Func<int,bool> funss= x=>x >7;
            int m = intarem.First<int>(funss);  //predicate passing to funcation  
            Console.WriteLine(m);

            int[] arrint = new int[3] { 47, 98, 58 };
            int value = arrint.First<int>(s => s > 50);

            Console.WriteLine(value);
            Console.ReadKey();       
        }
    }
Yes,We have many Extension methods in .net framework.we will see some of extension methods,and as well as
Parameters of Fun<int,bool> of Fun<T,TSource> of type
And
Parameters of Fun<T>.First() and Fun<T>.First(delegate)
Here delegate-àFun<T,Tsource>--àan lamda expression.
Note:Predicate means one condition[we are writing it as a lamda expression] to Sort/filter/check for our requirement from remaing elements.

No comments:

Post a Comment