了解 C# 代码机制

本文关键字:机制 代码 了解 | 更新日期: 2023-09-27 18:37:24

我是C#的新手,在我的教科书中,它要求我在程序.cs类的主方法中添加一些代码,但没有告诉我如何。我是一个初学者,所以我只是在寻找基础知识,随着我的前进,我将学习更高级的课程,所以请保持你的解释彻底,但要下降到第一天的水平。以下是我提供的代码。它不断为我提供错误<</p>

下面是代码: 我应该将公共静态语音TestIfElse方法添加到Program.cs类中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ifelse_Statement
{
    class Program
    {
        static void Main(string[] args)
        {
            TestIfElse(10);
            public static void TestIfElse(int n)
            {
                if (n < 10)
                {
                    Console.WriteLine(“n is less than 10”);
                }
                else if (n < 20)
                {
                    Console.WriteLine(“n is less than 20”);
                }
                else if (n < 30)
                {
                    Console.WriteLine(“n is less than 30”);
                }
                else
                {
                    Console.WriteLine(“n is greater than or equal to 30”);
                }
            }
        }
    }
}

了解 C# 代码机制

你的错误很简单 - 你不能在 C# 中使用嵌套函数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ifelse_Statement
{
    class Program
    {
        static void Main(string[] args)
        {
            TestIfElse(10);
        }
        public static void TestIfElse(int n)
        {
            if (n < 10)
            {
                Console.WriteLine(“n is less than 10”);
            }
            else if (n < 20)
            {
                Console.WriteLine(“n is less than 20”);
            }
            else if (n < 30)
            {
                Console.WriteLine(“n is less than 30”);
            }
            else
            {
                Console.WriteLine(“n is greater than or equal to 30”);
            }
        }
    }
}

您应该将 TestIfElse 方法的主体移到 Main 方法的主体之外。这样,它们都被视为类的方法,并且将按预期工作。

此外,在您的逻辑中,您需要检查范围,即小于 20 的数字也小于 30,因此您可能想检查一个数字是否在 10 到 20 之间,或者 20 到 30 之间等等。

static void Main(string[] args)
    {
        TestIfElse(10);
    }
public static void TestIfElse(int n)
{
    if (n < 10)
    {
        Console.WriteLine(“n is less than 10”);
    }
    else if (n < 20)
    {
        Console.WriteLine(“n is less than 20”);
    }
    else if (n < 30)
    {
        Console.WriteLine(“n is less than 30”);
    }
    else
    {
        Console.WriteLine(“n is greater than or equal to 30”);
    }
}

类这是一个类的基本结构

public class MyClass // this is the declaration of the class 
{
    // this is a property, it is accessible by things outside of this class.
    public static string MyProperty { get; set; } ;
    private static string _myField; // this is a ['private] field, it is intended to store the state of the object. it cannot be accessed from outside of this class
    static void Main(string[] args)
    {
        // this is the method that gets run first so it make all of your initial calls
    }
    public static void TestIfElse(int n)
    {
        // this is another method (taught as module, operation, action, or subroutine in schools)
        // it has return type of void which is more or less "nothing". This type of behavior simply does 
        // something but doesn't return a value
    }
    public static bool IsNotPrime(int input)
    {
        // this is an actual function in that will return a single value whether its a primitive value or an
        // object. Whatever it is, there's ONE. The point is that a call to this function is now synonymous with
        // the value it returns. So for example, if this method was real, it is equivalent to 'true' so you could 
        // actually say if(IsNotPrime(8)){ // do things }
        return input % 2 == 0;
    }
}

必须将这些方法分开。类可以有许多成员(字段、属性、方法等),一个方法可以调用另一个方法,但 on 方法不能包含另一个。因此,当你学习识别这些时,如果你看到诸如公共或静态之类的关键字,你应该认为这些需要在类内部成为它们自己的实体,但在其他类成员之外。

例如,除了类声明

本身之外,在声明类方法时,首先看到的是类型和标识符之前的privateprivate static(或任何其他访问修饰符),因此不要尝试在该成员中放置任何其他类似的东西。

您在使用代码时遇到的问题主要是关于TestIfElse的声明。你在主函数中声明了它,这是你不能做的。把它移到主楼外面,你应该没事。