如何在 C# 中使用“param”來獲取這個示例? (How can I get this example using "param" in C# to work?)


問題描述

如何在 C# 中使用“param”來獲取這個示例? (How can I get this example using "param" in C# to work?)

I am trying to understand the meaning and use of the param parameter in this line taken from a RelayCommand example:

return new RelayCommand(param => MessageBox.Show("It worked."));

First, I understand that the "param" parameter has nothing to do with the "params" keyword, is this correct?

public int Add(params int[] list)
{
  int sum = 0;
  foreach (int i in list)
    sum += i;
  return sum;
}

Second, what kind of delegate code do I have to add to get the following example to work?

using System;
using System.Collections.Generic;

namespace TestParam222
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The total is {0}.", Tools.GetTest(param => 23));
            Console.ReadLine();
        }
    }

    class Tools
    {
        public static string GetTest(List<int> integers)
        {
            return "ok";
        }
    }
}

‑‑‑‑‑

參考解法

方法 1:

param isn't a keyword. It's the parameter for a lambda expression in your sample. You'd need to make your method take a delegate or an expression tree, e.g.

using System;
using System.Collections.Generic;

namespace TestParam222
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("The total is {0}.", Tools.GetTest(param => 23));
      Console.ReadLine();
    }
  }

  class Tools
  {
    public static string GetTest(Func<int, int> integers)
    {
      return "ok";
    }
  }
}

The Func<int,int> could actually be any Func<T,int> (or Func<T,long> etc) because your lambda expression doesn't use param anywhere. Alternatively it could be an Expression<Func<int,int>> etc.

I suggest you read up on lambda expressions for more details, for instance in any of these SO questions:

  • Can you explain lambda expressions
  • What is a lambda
  • C# lambda expression, why should I use this

(by Edward TanguayJon Skeet)

參考文件

  1. How can I get this example using "param" in C# to work? (CC BY‑SA 3.0/4.0)

#lambda #C#






相關問題

Lambda 表達式中的 SQL WHERE 等價物是什麼? (What's the equivalence of an SQL WHERE in Lambda expressions?)

如何將 lambda 傳遞給 Razor 輔助方法? (How to pass in a lambda to a Razor helper method?)

lỗi biểu thức lambda: biểu thức phải là giá trị có thể sửa đổi (lambda expression error: expression must be a modifiable lvalue)

如何在 PySpark 中有效地按值排序? (How to sort by value efficiently in PySpark?)

將列表列表減少為字典,以子列表大小為鍵,出現次數為值 (Reduce list of list to dictionary with sublist size as keys and number of occurances as value)

匿名類作為泛型參數 (anonymous class as generic parameter)

如何為 lambda 中的運算符賦予不定性? (How to give infixities to operators in lambda?)

如何發出委託或 lambda 表達式 (Howto emit a delegate or lambda expression)

深入學習 C# 表達式樹的最佳資源是什麼? (What is the best resource for learning C# expression trees in depth?)

根據最高日期從 IGrouping 中獲取項目 (Get an item from a IGrouping based on the highest date)

如何在 C# 中使用“param”來獲取這個示例? (How can I get this example using "param" in C# to work?)

如何使用 C# 中的 Lambda 表達式僅返回沒有來自另一個表的行的行 (How to return only rows where no rows from another table using Lambda Expressions in C#)







留言討論