4

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

juniorCSharp
  • 97
  • 1
  • 6

3 Answers3

3

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
  • 111,048
  • 26
  • 262
  • 307
1

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
  • 64,563
  • 18
  • 145
  • 216
0

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
  • 134,492
  • 47
  • 225
  • 308