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

IT1050课程编程新手求助:.NET Fiddle中void函数使用故障排查

Troubleshooting Void Function Issues in .NET Fiddle

Hey Cole! I see you're just starting out with programming in your IT1050 course and hitting a snag with void functions in .NET Fiddle—let's get this sorted out for you.

First, let's break down the key issues that are keeping your code from running, which your instructor already hinted at:

  • Function placement: In the style of C# your course is teaching, you shouldn’t define functions inside the Main method. Functions need to live at the class level (directly inside the Program class, outside of Main).
  • Missing method modifiers: Since Main is a static method, any function you call from it also needs to be static (adding public as your instructor suggested makes it properly accessible from Main).

Here's the fixed version of your code that will run perfectly in .NET Fiddle:

using System;
public class Program {
    public static void Main() {
        // Function - 包含一系列语句的代码块。程序通过调用函数并指定所需参数来执行这些语句
        // void function - 没有返回值的函数
        // function type - 函数返回值的类型
        // function name - 调用函数时使用的名称
        // parameter - 传递给函数的信息,在函数内部相当于变量
        greeting("Cole");
        greeting("John");
    }

    // Moved outside Main and added required public static modifiers
    public static void greeting(String name) {
        Console.WriteLine("Hello " + name);
    }
}

What exactly changed?

  1. I relocated the greeting function out of the Main method and placed it directly inside the Program class. This fixes the scope problem where the function wasn’t being recognized correctly.
  2. Added public static before void greeting—since Main is static, it can only call other static methods in the same class without creating an instance of the class first.

When you run this code, you’ll see the expected output:

Hello Cole
Hello John

That should get your code working! Feel free to ask if you have more questions as you keep learning programming.

内容的提问来源于stack exchange,提问作者Cole Witherspoon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 08:47:41