Explain Linq Query in C#

Shervan360 1,011 Reputation points
2021-11-11T04:44:44.963+00:00

Hello,

I understand the below query.

      static bool isUppercase(string s)
        {
            return s.All(x=> char.IsUpper(x));
        }

But I don't understand the following:

            static bool isUppercase(string s)
            {
                  return s.All(char.IsUpper); //It's a little confusing.
            }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,019 questions
No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,341 Reputation points
    2021-11-12T02:13:54.54+00:00

    Hi @Shervan360 ,

    s.All(char.IsUpper);

    Take a look at:

    1. What is a method group in C#?
    2. C# method group strangeness

    Hope them could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

  2. Karen Payne MVP 29,236 Reputation points Microsoft MVP
    2021-11-12T14:27:23.923+00:00

    Look at Intellisense and then look at the docs Char.IsUpper

    148866-figure1.png

    I would make this an extension method

    public static class StringExtensions  
    {  
        public static bool IsUppercase(this string s) => s.All(char.IsUpper);  
    }  
    

    Then write a test method

    [TestMethod]  
    public void IsAllTest()  
    {  
        var name = "karen";  
        Assert.IsFalse(name.IsUppercase());  
      
        name = "KAREN";  
        Assert.AreEqual(name, "KAREN");  
    }  
    

    This might make more sense

    public static class StringExtensions  
    {  
        public static bool IsUppercase(this string source) => source.All(char.IsUpper);  
        public static bool IsUpperCase(this string source) => (from character in source where char.IsUpper(character) select character).Count() == source.Length;  
    }  
    
    
    [TestMethod]  
    public void IsAllTest()  
    {  
        var name = "karen";  
        Assert.IsFalse(name.IsUpperCase());  
      
        name = "KAREN";  
        Assert.IsTrue(name.IsUpperCase());  
    }  
    

    Then back to this which uses a method group and is harder for novice developers to grasp

    [DebuggerStepThrough]  
    public static bool IsUpperCase(this string source) =>   
        (source.Where(char.IsUpper)).Count() == source.Length;  
    
    No comments

  3. Bruce (SqlWork.com) 28,276 Reputation points
    2021-11-12T16:07:02.37+00:00

    It’s really pretty simple. While you can pass a lambda in a linq expression you can also just pass a function that takes the correct argument type:

    static bool IsUpper(char c)
    {
          return char.IsUpper(c);
    }
    
    static bool isUppercase(string s)
    {
         return s.All(IsUpper);
    }
    

    But there is no need to write the first method because char.ToUpper has the same signature.

    No comments