【C#】Microsoftlearn入门C#教程:01.Write Your First Code Using C#


Learn / Training / Browse / Write your first C# code /Learn how itworks

1. 什么是编程语言

What is a programming language?

编程语言如C#允许你编写希望计算机执行的指令。每种编程语言都有自己的语法,但在学习了第一种编程语言并尝试学习另一种语言后,你会很快意识到它们都有许多相似的概念。编程语言的工作是让人以人类可读和可理解的方式表达他们的意图。你用编程语言编写的指令称为"源代码"或简称"代码"。软件开发人员编写代码。

Programming languages like C# let you write instructions that you want the computer to carry out. Each programming language has its own syntax, but after learning your first programming language and attempting to learn another one, you’ll quickly realize that they all share many similar concepts. A programming language’s job is to allow a human to express their intent in a human-readable and understandable way. The instructions you write in a programming language are called “source code” or just “code”. Software developers write code.

此时,开发人员可以更新和更改代码,但计算机无法理解代码。代码必须先编译成计算机可以理解的格式。

At this point, a developer can update and change the code, but the computer can’t understand the code. The code first must be compiled into a format that the computer can understand.

2. 什么是编译

What is compilation?

一个名为编译器的特殊程序将源代码转换为计算机的中央处理器(CPU)可以执行的不同格式。当你在之前的单元中使用绿色的运行按钮时,你编写的代码首先被编译,然后被执行。

A special program called a compiler converts your source code into a different format that the computer’s central processing unit (CPU) can execute. When you used the green Run button in the previous unit, the code you wrote was first compiled, then executed.

为什么代码需要编译?虽然大多数编程语言起初看起来很神秘,但它们比计算机的首选语言更容易被人理解。CPU理解通过打开或关闭数千或数百万个微小开关来表示的指令。编译器通过将人类可读的指令转换为计算机可理解的指令集来连接这两个世界。

Why does code need to be compiled? Although most programming languages seem cryptic at first, they can be more easily understood by humans than the computer’s preferred language. The CPU understands instructions that are expressed by turning thousands or millions of tiny switches either on or off. Compilers bridge these two worlds by translating your human-readable instructions into a computer-understandable set of instructions.

3. 什么是语法

What is syntax?

编写C#代码的规则称为语法。就像人类语言有标点符号和句子结构的规则一样,计算机编程语言也有规则。这些规则定义了C#的关键字和运算符,以及如何将它们组合在一起形成程序。

The rules for writing C# code is called syntax. Just like human languages have rules regarding punctuation and sentence structure, computer programming languages also have rules. Those rules define the keywords and operators of C# and how they are put together to form programs.

当你在.NET编辑器中编写代码时,你可能已经注意到不同单词和符号的颜色发生了细微的变化。语法高亮是一个有用的功能,你将开始使用它来轻松发现代码中不符合C#语法规则的错误。

When you wrote code into the .NET Editor, you may have noticed subtle changes to the color of different words and symbols. Syntax highlighting is a helpful feature that you’ll begin to use to easily spot mistakes in your code that don’t conform to the syntax rules of C#.

4. 你的代码是如何工作的

How did your code work?

让我们关注你编写的以下代码行:

Let’s focus on the following line of code you wrote:

Console.WriteLine("Hello World!");

console.writeLine prints the output on the existing line and appends a new lineafter it.

当你运行代码时,你看到消息Hello World!被打印到输出控制台。当短语在C#代码中被双引号包围时,它被称为文字字符串。换句话说,你想要字符H、e、l、l、o等被发送到输出。

When you ran your code, you saw that the message Hello World! was printed to the output console. When the phrase is surrounded by double-quotation marks in your C# code, it’s called a literal string. In other words, you literally wanted the characters H, e, l, l, o, and so on, sent to the output.

Console部分称为类。类"拥有"方法;或者你可以说方法存在于类中。要访问方法,你必须知道它在哪个类中。现在,将类视为表示对象的一种方式。在这种情况下,对输出控制台进行操作的所有方法都定义在Console类中。

The Console part is called a class. Classes "own" methods; or you could say that methods live inside of a class. To visit the method, you must know which class it’s in. For now, think of a class as a way to represent an object. In this case, all of the methods that operate on your output console are defined inside of the Console class.

还有一个点(或句点)将类名Console和方法名WriteLine()分隔开。点是成员访问运算符。换句话说,点是你从类"导航"到其方法之一的方式。

There’s also a dot (or period) that separates the class name Console and the method name WriteLine(). The period is the member access operator. In other words, the dot is how you "navigate" from the class to one of its methods.

WriteLine()部分称为方法。你总是可以通过它后面的一组括号来识别方法。每个方法都有一个工作。WriteLine()方法的工作是将一行数据写入输出控制台。要打印的数据作为输入参数在开括号和闭括号之间发送。某些方法需要输入参数,而其他方法不需要。但如果要调用方法,必须始终在方法名称后使用括号。括号称为方法调用运算符

The WriteLine() part is called a method. You can always spot a method because it has a set of parentheses after it. Each method has one job. The WriteLine() method’s job is to write a line of data to the output console. The data that’s printed is sent in between the opening and closing parenthesis as an input parameter. Some methods need input parameters, while others don’t. But if you want to invoke a method, you must always use the parentheses after the method’s name. The parentheses are known as the method invocation operator.

最后,分号是语句结束运算符。语句是C#中的完整指令。分号告诉编译器你已经完成了命令的输入。

Finally, the semicolon is the end of statement operator. A statement is a complete instruction in C#. The semicolon tells the compiler that you’ve finished entering the command.

如果所有这些想法和术语都没有意义,不要担心。现在,你需要记住的是,如果要将消息打印到输出控制台:

Don’t worry if all of these ideas and terms don’t make sense. For now, all you need to remember is that if you want to print a messag e to the output console:

  • Use Console.WriteLine(“Your message here”);
  • Capitalize Console,
  • Write, and Line Use the correct punctuation because it has a special
    role in C#
  • If you make a mistake, just spot it, fix it and re-run

Tip Create a cheat sheet(备忘单) for yourself until you’ve memorized certain
key commands

Common mistakes new programmers make:

  • Entering lower-case letters instead of capitalizing C in Console, or the letters W or L in WriteLine.
  • Entering a comma instead of a period between Console and WriteLine.
  • Forgetting to use double-quotation“√” marks, or using single-quotation '×' marks to surround the phrase Hello World!
  • Forgetting a semi-colon at the end of the command.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmywillstronger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值