1
    public bool ContainsUnicodeCharacter(char[] input)
    {
        const int MaxAnsiCode = 255;
        bool temp;
        string s;

        foreach (char a in input)
        {
            s = a.ToString();
            temp = s.Any(c => c > MaxAnsiCode);

            if (temp == false)
            {
                return false;
            }
        }            
    }

This code used to check unicode exist or not on input char array.

I got error message : " ContainsUnicodeCharacter(char[])': not all code paths return a value"

What went wrong here, Please help. Thank You.

4 Answers4

5

Your method is not well thought through. It can be done much simpler:

public static bool ContainsUnicodeCharacter(this IEnumerable<char> input)
{
    const int MaxAnsiCode = 255;
    return input.Any(c => c > MaxAnsiCode);
}

You had two nested loops there without reason.

I made the method a generally applicable extension method.

usr
  • 168,620
  • 35
  • 240
  • 369
  • 2
    If you made the parameter `input` have type `IEnumerable`, the method could easily be called with a string or any collection of `char` values. Also, that signature will signal to the callers: "We won't change your `input`, we'll just loop through it." – Jeppe Stig Nielsen May 30 '13 at 15:30
  • This is another solution without using lambda expresions http://stackoverflow.com/a/40252987/348551 – Yiannis Mpourkelis Oct 27 '16 at 10:22
3

You need to add return true; just before the last }, but I also think you have the test reversed:

public bool ContainsUnicodeCharacter(char[] input)
{
    const int MaxAnsiCode = 255;
    bool temp;
    string s;

    foreach (char a in input)
    {
        s = a.ToString();
        temp = s.Any(c => c > MaxAnsiCode); // true if unicode found

        if (temp == true)
        {
            return true;
        }
    }

    return false;
}
egrunin
  • 24,650
  • 8
  • 50
  • 93
3

Further to @egrunin's answer, I don't know why you loop through all of the characters and then cast them to a string, just so you can use a Linq method on the resulting character arrayt. You could simplify your entire method (maintaining the same logic) like this:

public bool ContainsUnicodeCharacter(char[] input)
{
    const int MaxAnsiCode = 255;

    return input.Any(c => c > MaxAnsiCode);
}
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
0

You only have 1 return statement which is part if a conditional if...as mentioned you can add return true; but the reason you got that error is that your function had nothing to return if temp never equated to false

public bool ContainsUnicodeCharacter(char[] input)
    {
        const int MaxAnsiCode = 255;
        bool temp;
        string s;

        foreach (char a in input)
        {
            s = a.ToString();
            temp = s.Any(c => c > MaxAnsiCode);
            if (temp == false)
            {
               return false;
             }
        } 
         return true;  
    }
curtisk
  • 19,950
  • 4
  • 55
  • 71