Remove anonymous function

Last post 11-05-2009 6:26 PM by Paul Linton. 4 replies.

Sort Posts:

  • Remove anonymous function

    11-05-2009, 6:50 AM
    • Member
      459 point Member
    • duttavr
    • Member since 06-23-2006, 8:21 AM
    • Hyderabad, India
    • Posts 162
                //Sample 1 - Using Labmda Expression 
    String str1 = "XXXwww.msdotnetsupport.blogspot.comXXX";
    foreach (var item in str1.Where(x => x != 'X'))
    {
    Console.Write(item);
    }



    //Sample 2 - without Labmdas, and using ananymous method.
    String str2 = "XXXwww.msdotnetsupport.blogspot.comXXX";
    foreach (var item in str2.Where<char>(delegate(char x)
    {
    return x != 'X';
    }))
    {
    Console.Write(item);
    }



    //Sample 3
    Now I do not want to use ananymouse delegate method as parameter to Where function
    so can anybody can help me how to convert sample2 to sample3 without using anonymous function and map the parameter to below function

    public static bool CharCheck(char x)
    {
    return x != 'X'
    }
    Please mark as answer if my post is useful and Please correct me if I am wrong anything here.
  • Re: Remove anonymous function

    11-05-2009, 7:55 AM
    Answer
    foreach (var item in str2.Where<char>(CharCheck))


     

  • Re: Remove anonymous function

    11-05-2009, 5:06 PM
    • Contributor
      4,980 point Contributor
    • Paul Linton
    • Member since 04-29-2008, 11:16 PM
    • Posts 870

    I think the previous poster meant

    foreach (char letter in str2.Where<char>(x=>CharCheck(x)) )


    (of course, your exact example could just be Console.Write(str2.Replace("X", string.Empty)) ) 

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
  • Re: Remove anonymous function

    11-05-2009, 5:21 PM

    Paul Linton:

    I think the previous poster meant

     

    No, I actually tried that and it worked Smile.  As long as the method matches the necessary delegate, it works.

  • Re: Remove anonymous function

    11-05-2009, 6:26 PM
    • Contributor
      4,980 point Contributor
    • Paul Linton
    • Member since 04-29-2008, 11:16 PM
    • Posts 870

    Sorry, my mistake.

    Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
Page 1 of 1 (5 items)