Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# static keyword inside LINQ Select, Where,

Tags:

c#

linq

I noticed it is valid to add the static keyword inside Linq expressions. However it doesn't generate any different code.

Example:

list.Select(static model => model.Age).ToList();
list.Where(static model => model.Age > 18).ToList();

What is the purpose of the static keyword in this context?

See the generated code on Sharplab

Generated code according to sharplab:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public class Class
{
    [Serializable]
    [CompilerGenerated]
    private sealed class <>c
    {
        public static readonly <>c <>9 = new <>c();

        public static Func<Model, bool> <>9__2_0;

        public static Func<Model, bool> <>9__2_1;

        internal bool <A>b__2_0(Model model)
        {
            return model.Age > 10;
        }

        internal bool <A>b__2_1(Model model)
        {
            return model.Age > 10;
        }
    }

    private List<Model> list = new List<Model>();

    public Class()
    {
        List<Model> obj = list;
        Model model = new Model();
        model.Age = 10;
        obj.Add(model);
    }

    public void A()
    {
        List<Model> value = Enumerable.ToList(Enumerable.Where(list, <>c.<>9__2_0 ?? (<>c.<>9__2_0 = new Func<Model, bool>(<>c.<>9.<A>b__2_0))));
        List<Model> value2 = Enumerable.ToList(Enumerable.Where(list, <>c.<>9__2_1 ?? (<>c.<>9__2_1 = new Func<Model, bool>(<>c.<>9.<A>b__2_1))));
        Console.WriteLine(value);
        Console.WriteLine(value2);
    }
}
public class Model
{
    public int Age;
}
like image 665
Matthiee Avatar asked Oct 22 '25 10:10

Matthiee


1 Answers

Methods allow local inner methods, which are generally used as helpers to wrap work in separate blocks. They can also be marked as static, which means that the helper method will not have access to local variables or parameters within the outer enclosing method. So when you're using enumerator expressions like:

list.Select(model => model.Age)

The block model => model.Age is a lambda expression that represents something like a local helper method. The static keyword, similar to its usage in local methods or functions, means that this expression cannot access variables, parameters, or any non-static (or constant) values declared outside of itself.

For example, the following code will not compile:

int dummy = 0;
list.Select(static model =>
{
    dummy++; // CS8820: A static anonymous function cannot contains ref[...]
    return model.Age;
})
like image 132
James Jonatah Avatar answered Oct 25 '25 02:10

James Jonatah