You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#委托使用报错:名称在当前上下文中不存在

解决C#委托中“名称在当前上下文中不存在”的错误

Hey there, let's work through that "名称在当前上下文中不存在" error you're hitting with delegates in your C# code. Looking at your snippet, there are a few common culprits here—let's break them down with fixes tailored to your code structure.

首先,补全你的代码片段

First off, your InformationOutput method is cut off, and since Person implements the Method1 interface, you need to make sure all interface methods are fully implemented. Let's start by fixing that:

using System;
namespace Laboratory_work {
    public delegate void Delegate();
    public interface Method1 {
        string InformationOutput(Person obj);
        int WorkOffer();
    }
    public interface Method2 {
        int Invitation();
    }
    public abstract class Person: Method1 {
        public string Name{get; set;}
        public string Surname{get; set;}
        public int Age{get; set;}
        public virtual string InformationOutput(Person obj) {
            string info = "Some personal information \n_________________________\n\n" + 
                          $"Full name: {obj.Name} {obj.Surname}\nAge: {obj.Age}";
            return info;
        }
        // 必须实现接口的WorkOffer方法(抽象类可以留作抽象)
        public abstract int WorkOffer();
    }
}

常见错误原因及修复

1. 委托签名与目标方法不匹配

Your delegate public delegate void Delegate(); expects a method with no parameters and a void return type. But the methods in your interfaces (InformationOutput, WorkOffer, Invitation) all have parameters or return values—so you can't directly assign those to this delegate. If you tried to do something like Delegate del = InformationOutput;, that's not only a signature mismatch but would also throw an error if you didn't reference an instance of the class.

Fix: Either add a method that matches the delegate's signature, or adjust the delegate to match the method you want to use.

Example of adding a matching method to Person:

public abstract class Person: Method1 {
    // ... existing properties and methods ...
    
    // 新增一个符合Delegate签名的方法
    public void PrintWelcomeMessage() {
        Console.WriteLine("Welcome to the system!");
    }
}

Then use it correctly:

class Program {
    static void Main(string[] args) {
        // 创建Person的具体子类实例(因为Person是抽象类)
        class ConcretePerson : Person, Method2 {
            public override int WorkOffer() => 4000;
            public int Invitation() => 1;
        }
        
        ConcretePerson person = new ConcretePerson { Name = "Alice", Surname = "Smith", Age = 28 };
        
        // 实例化委托并绑定到匹配的方法
        Delegate myDelegate = person.PrintWelcomeMessage;
        
        // 调用委托
        myDelegate();
    }
}

If you want to use InformationOutput with a delegate, adjust the delegate's signature:

// 定义匹配InformationOutput的委托
public delegate string InfoDelegate(Person obj);

// 使用时:
InfoDelegate infoDelegate = person.InformationOutput;
string personInfo = infoDelegate(person);
Console.WriteLine(personInfo);

2. 直接调用委托类型而非实例

If you tried to write Delegate(); directly without creating an instance of the delegate first, that would trigger the "名称不存在" error—you're trying to call the delegate type as if it's a method, which isn't valid.

Fix: Always instantiate the delegate with a valid method first, then call the instance.

3. 方法名称拼写错误或作用域问题

Double-check that the method name you're assigning to the delegate is spelled correctly, and that the method is accessible in your current scope. For instance:

  • If the method is private, you can't reference it from outside the class.
  • If it's an instance method, you need to have an instance of the class to bind it to (not just the class name).

内容的提问来源于stack exchange,提问作者Настя Шпит

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:36:01