tags:

views:

99

answers:

4

Hi

Below function working ok but I want to make it simple.

if (list.Exists(delegate(string s) { return s.Contains(str); }))
{
    string name = list.Find(delegate(string s) { return s.Contains(str); });
}

I am using delegate(string s) { return s.Contains(str); } two times Is there any way to make this simple. I know how to create delegate but don't know how to use it.

    //create delegate       
    public delegate bool nameExistsDelegate(List<string> list, string name);

    // Create a method for a delegate.
    public static bool IsnameExists(List<string> list, string name)
    {
        return list.Exists(delegate(string s) { return s.Contains(name) ; });
    }

    // Create a method for a delegate.
    public static string GetName(List<string> list, string name)
    {
        return list.Find(delegate(string s) { return s.Contains(name) ; });
    }

UPDATE

stuck with .NET 2.0 so I can't use LINQ

+1  A: 

if you're on .net 3.5 you can use lamdas

 //create delegate       
    public delegate bool nameExistsDelegate(List<string> list, string name);

    static Func<string, bool> exists = s =>  return s.Contains(name);

    // Create a method for a delegate.
    public static bool IsnameExists(List<string> list, string name)
    {
        return list.Exists(s => exists(s));
    }

    // Create a method for a delegate.
    public static string GetName(List<string> list, string name)
    {
        return list.Find(s => exists(s));
    }
Preet Sangha
+2  A: 

There is a Func<,> exactly for this purpose:

Predicate<string> f = s => s.Contains(str); //<-- I prefer this syntax

if (list.Exists(f))
{
    string name = list.Find(f);
}
Travis Gockel
@Ani: Why do you think the lambda expression is typed `Func<string, bool>`? Lambdas don't have a type. A *lambda expression* can be *converted* to a compatible delegate or expression tree type.
Mehrdad Afshari
@Mehrdad Afshari: I didn't say anything about lambdas being typed. What I was pointing out had to do with the lack of convertibility between seemingly similar delegate *types*. The code was `Func<string, bool> f = s => s.Contains(str)` before it was edited.
Ani
@Ani: Oh, sorry. Didn't notice that the answer is edited.
Mehrdad Afshari
Yeah, I forgot that `Exists` takes a `Predicate<T>`, not a `Func<T, bool>`
Travis Gockel
+2  A: 

The anonymous method you're using will be converted to a Predicate<string> delegate by the compiler. With this in mind, you can introduce a local to get rid of the redundancy you don't want.

Predicate<string> containsStr = delegate(string s) { return s.Contains(str); };

if (list.Exists(containsStr))
{
   string name = list.Find(containsStr);
   ...
}

In C# 3.0 or later, you can express this even more succintly with lambda-expressions.

Predicate<string> containsStr = s => s.Contains(str);

On another note, you don't need to first test that str exists and then proceed to find it (assuming the list doesn't contain nulls), you could just do:

string name = list.Find(s => s.Contains(str));
if(name != null)
{
   //found
}

Of course, I should also point out that strings don't contain any extra meta-data other than the characters present in them, so you don't gain anything by 'finding' a string in a list over just proving it exists (unless you meantFindIndex).

Ani
+1 you beat me in 50 mins.LOL
Danny Chen
A: 

I'd recommend reading up on the standard delegate types in C#

Here you actually need a Predicate, which takes in an object, tests it with some condition and returns a pass/fail result.

Predicate<string> containsCheck = item = > item.Contains(str);
if (list.Exists(containsCheck)
{
    string name = list.Find(containsCheck);
}

Note: all of the code can also be done using LINQ, which is considerable simpler. But I guess you must be learning delegates right now.. JFYI

using System.Linq;
...
Predicate<string> substringCheck = item => item.Contains(str);
            var exists = list.Any(substringCheck);
            var getMatch = list.First(substringCheck);
Gishu