C# - 如何将 List<T> 作为参数传递给委托
C# - How to pass List<T> to a delegate as parameter
本质上我想做的是创建一个委托,我想将函数传递给一个方法——它将获取产品对象列表并执行某些计算。
很遗憾,我收到一条错误消息
main.cs(11,40):错误 CS0120:需要对象引用才能访问非静态成员“MainClass.MyDelegate(System.Collections.Generic.List)”
编译失败:1 个错误,0 个警告
编译器退出状态 1
我的问题是为什么我很难将列表传递给代表 - 我做错了什么?
谢谢你和亲切的问候
using System;
using System.Collections.Generic;
using System.Collections;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine ("Hello World");
MyCart MyBasket = new MyCart();
MyBasket.Initialize();
//Console.WriteLine(MyBasket.basket.Count);
MyBasket.BuyItem("Iphone", 300.3m, MyDelegate );
}
public void MyDelegate (List<Product> e){
// this is where i am performing my delegate
if (e.Count > 2)
{
Console.WriteLine($"This is delegate Talking our current Cart has more than 2 items inside, in fact the count is {e.Count}");
}
else
{
Console.WriteLine($"This is delegate Talking our current Cart has less than 2 items inside, in fact the count is {e.Count}");
}
}
}
public class MyCart
{
//public delegate void ObjectCheck(List<Product> basket) ;
public List<Product> basket = new List<Product>();
public delegate void ObjectCheck(List<Product> basket) ;
public void Initialize (){
// this is our Cart Class constructor
basket.Add(new Product { ItemName = "Book", ItemPrice = 4.9m });
basket.Add(new Product { ItemName = "Milk", ItemPrice = 3.5m });
}
public void BuyItem (string i, decimal p, ObjectCheck mymethod)
{
basket.Add(new Product {ItemName = i, ItemPrice = p});
mymethod(basket);
}
}
您遇到此错误是因为 MyDelegate
是一个实例方法(上面没有 static
关键字)并且它正在 static
上下文中使用(Main
).要修复错误,您需要这样声明 MyDelegate
:
public static void MyDelegate (List<Product> e)
{
// do some stuff
}
本质上我想做的是创建一个委托,我想将函数传递给一个方法——它将获取产品对象列表并执行某些计算。
很遗憾,我收到一条错误消息
main.cs(11,40):错误 CS0120:需要对象引用才能访问非静态成员“MainClass.MyDelegate(System.Collections.Generic.List)” 编译失败:1 个错误,0 个警告 编译器退出状态 1
我的问题是为什么我很难将列表传递给代表 - 我做错了什么?
谢谢你和亲切的问候
using System;
using System.Collections.Generic;
using System.Collections;
class MainClass {
public static void Main (string[] args) {
Console.WriteLine ("Hello World");
MyCart MyBasket = new MyCart();
MyBasket.Initialize();
//Console.WriteLine(MyBasket.basket.Count);
MyBasket.BuyItem("Iphone", 300.3m, MyDelegate );
}
public void MyDelegate (List<Product> e){
// this is where i am performing my delegate
if (e.Count > 2)
{
Console.WriteLine($"This is delegate Talking our current Cart has more than 2 items inside, in fact the count is {e.Count}");
}
else
{
Console.WriteLine($"This is delegate Talking our current Cart has less than 2 items inside, in fact the count is {e.Count}");
}
}
}
public class MyCart
{
//public delegate void ObjectCheck(List<Product> basket) ;
public List<Product> basket = new List<Product>();
public delegate void ObjectCheck(List<Product> basket) ;
public void Initialize (){
// this is our Cart Class constructor
basket.Add(new Product { ItemName = "Book", ItemPrice = 4.9m });
basket.Add(new Product { ItemName = "Milk", ItemPrice = 3.5m });
}
public void BuyItem (string i, decimal p, ObjectCheck mymethod)
{
basket.Add(new Product {ItemName = i, ItemPrice = p});
mymethod(basket);
}
}
您遇到此错误是因为 MyDelegate
是一个实例方法(上面没有 static
关键字)并且它正在 static
上下文中使用(Main
).要修复错误,您需要这样声明 MyDelegate
:
public static void MyDelegate (List<Product> e)
{
// do some stuff
}