C# entry development (Hello World, operator)

C# entry development

Introduction: This article explains the introductory development of C#.

explain

Write "Hello World"

When explaining the basic syntax of C#, we can start with the simplest "Hello World!" program. Here is an example that prints "Hello World!":

using System;

class Program
{
    
    
    static void Main()
    {
    
    
        Console.WriteLine("Hello World!");

        Console.ReadLine();
    }
}

This procedure involves several basic concepts:

  • using System;: This is a namespace declaration, specifying that we want to use Systemthe types under the namespace in the program. SystemThe namespace contains some commonly used types and methods.

  • class Program: This is the definition of a class, we call this class Program.

  • static void Main(): This is a main method (Main method), which is the entry point of the program. When the program runs, Main()the method is called automatically. staticThe keyword indicates that the method is a static method and can be called without instantiating an object; voidit indicates that the method does not return any value.

  • Console.WriteLine("Hello World!");: This is an output statement that prints the text "Hello World!" to the console window.

  • Console.ReadLine();: This statement waits for user input at the console, and continues to execute the following code after pressing the Enter key. This is done to prevent the console window from closing immediately after printing "Hello World!".

By writing this simple "Hello World!" program, we can begin to understand the basic syntax of C#. If you have any further questions, please feel free to ask!

basic operator

As we move on to the basic syntax of C#, we can introduce variables, data types, and basic operators. Here is an example that calculates the sum of two numbers and prints the result:

using System;

class Program
{
    
    
    static void Main()
    {
    
    
        // 声明并初始化两个整型变量
        int num1 = 10;
        int num2 = 20;

        // 计算两个数的和
        int sum = num1 + num2;

        // 输出结果
        Console.WriteLine("num1: " + num1);
        Console.WriteLine("num2: " + num2);
        Console.WriteLine("sum: " + sum);

        Console.ReadLine();
    }
}

This example involves several basic concepts:

  • int: This is the data type that represents integers in C#.

  • = : This is the assignment operator, which is used to assign the value on the right to the variable on the left.

  • +: This is the addition operator, used in this example to calculate the sum of two numbers.

在这个示例中,我们声明并初始化了两个整型变量num1num2,分别赋值为10和20。接着,我们使用加法运算符将num1num2相加得到和,存储在新的整型变量sum中。

最后,我们使用Console.WriteLine()方法输出三个变量的值。

通过这个例子,您可以更深入地了解C#的基本语法,包括变量的声明、赋值操作符和基本运算符。如果您有任何进一步的问题,请随时提问!

当继续讲解C#的基础语法时,我们可以介绍循环结构。以下是一个使用循环计算数字的阶乘的示例:

using System;

class Program
{
    
    
    static void Main()
    {
    
    
        // 接收用户输入的数字
        Console.Write("请输入一个整数:");
        int number = Convert.ToInt32(Console.ReadLine());

        // 计算数字的阶乘
        int factorial = 1;
        for (int i = 1; i <= number; i++)
        {
    
    
            factorial *= i;
        }

        // 输出结果
        Console.WriteLine("{0} 的阶乘是 {1}", number, factorial);

        Console.ReadLine();
    }
}

这个例子涉及到几个基本概念:

  • for循环:for循环用于重复执行一段代码,它由一个初始化语句、一个条件表达式和一个迭代语句组成。在这个例子中,我们使用for循环计算数字的阶乘。

  • {0}{1}:这些是格式化字符串中的占位符,用于在输出语句中插入变量的值。numberfactorial分别被替换为占位符{0}{1}的对应值。

在这个示例中,我们首先使用Console.Write方法向用户显示提示信息,要求用户输入一个整数。然后,使用Console.ReadLine方法接收用户输入的数据,并使用Convert.ToInt32()方法将其转换为整数类型。

接下来,我们声明一个变量factorial并初始化为1。然后,使用for循环从1到输入的数字进行迭代,并在每次迭代中乘以当前的循环变量i

最后,使用Console.WriteLine方法输出结果,其中使用了格式化字符串来包含输入数字和计算出的阶乘结果。

Through this example, you can have a deeper understanding of the loop structure in C#, especially forthe usage of loops, and learn how to use loops to implement some common computing operations. If you have any further questions, please feel free to ask!

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/131217841