天天看點

2020年初學者終極C指南 基本運算符 (Basic Operators) 數學 (Math) 條件語句 (Conditional Statements) 循環 (Loops) 結論 (Conclusion)

C# is a high-level programming language. It was initially designed for developing programs for set-top boxes and handheld devices, but later became a popular choice for creating web applications. The C# syntax is similar to C++ but is strictly an object-oriented programming language.

C#是一種進階程式設計語言。 它最初是為開發機頂盒和手持裝置的程式而設計的,但後來成為建立Web應用程式的流行選擇。 C#文法類似于C ++,但嚴格來說是一種面向對象的程式設計語言。

I hope with this article that you’ll learn and understand the fundamentals of C#.

我希望通過本文,您将學習和了解C#的基礎知識。

入門 (Getting Started)

The first thing you need to do is install Microsoft Visual Studio, and you can start writing C# code. Once the IDE has been installed, you create a new C# console application.

您需要做的第一件事是安裝Microsoft Visual Studio ,然後就可以開始編寫C#代碼了。 安裝IDE後,您将建立一個新的C#控制台應用程式。

In your first file: Program.cs You will see the following code:

在第一個檔案:Program.cs中,您将看到以下代碼:

using System;namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello Medium!");    
    }
  }
}
           

Click the Run button to compile and run the code.

單擊“運作”按鈕以編譯并運作代碼。

Output: Hello 
           

句法 (Syntax)

The most common method to output something in C# is ‘WriteLine()’.

在C#中最常見的輸出方法是'WriteLine()'。

Console.WriteLine("Hello World!");  
Console.WriteLine("I will print on a new line.");Console.Write("Hello World! ");
Console.Write("I will print on the same line.");
           

The main method

主要方法

The main method is required in every C# program to run it, and it looks like this:

每個C#程式都需要main方法來運作它,它看起來像這樣:

static void Main(string[] args){}
           

注釋 (Comments)

Comments are commonly known around all programming languages to add notes to your code to explain it, in C# that works as follows:

注釋在所有程式設計語言中都是衆所周知的,它在C#中的工作原理如下:

// This is a comment
Console.WriteLine("Hello Medium!");
           

Multi-Line Commenting

多行評論

/* The code below will print the words Hello World
to the screen, and it is amazing */
Console.WriteLine("Hello Medium!");
           

變數 (Variables)

A variable is like a little container to store data in. Variables are essential in C#. In C#, you need to specify the data type of a variable.

變量就像一個用于存儲資料的小容器。變量在C#中是必不可少的。 在C#中,您需要指定變量的資料類型。

As in other programming languages, C# contains the following data types:

與其他程式設計語言一樣,C#包含以下資料類型:

  • String — A string is a sequence of characters

    String —字元串是字元序列

  • Integer — An integer data type is a non-decimal number

    整數—整數資料類型是非十進制數

  • Float — A float is a number with a decimal point or a number

    浮點數—浮點數是帶有小數點或數字的數字

  • Boolean — A Boolean represents TRUE or FALSE.

    布爾值—布爾值表示TRUE或FALSE。

  • Array — An array stores multiple values in one single variable.

    數組—數組将多個值存儲在一個變量中。

Declaring & Initializing a variable

聲明和初始化變量

You first name the type, for example, an integer, then enter the name of the variable, and if you want to initialise one, you can add value after the ‘=’ sign.

您首先命名類型,例如,整數,然後輸入變量的名稱,如果要初始化變量,可以在“ =”符号後添加值。

string name = "Bryan";
Console.WriteLine(name);
           

數組 (Arrays)

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

數組用于将多個值存儲在單個變量中,而不是為每個值聲明單獨的變量。

Declaring an Array

聲明一個數組

string[] cars;
           

Initialising the Array

初始化陣列

string[] cars = {"Ford", "Audi", "Tesla"};
           

Have access to the elements

可以通路元素

string[] cars = {"Ford", "Audi", "Tesla"};
Console.WriteLine(cars[0]);
// Outputs Ford
           

基本運算符 (Basic Operators)

  • + (Addition)

    +(加法)

  • - (Subtraction)

    -(減法)

  • * (Multiplication)

    *(乘法)

  • / (Division)

    /(部門)

  • % (Modules)

    %(模組)

  • ++ (Increment)

    ++(增量)

  • - -(Decrement)

    --(遞減)

數學 (Math)

C# has a math class that you can use to execute mathematic functions like this min() and max() functions or methods.

C#有一個數學類,您可以用來執行數學函數,例如min()和max()函數或方法。

Math.Max(5, 10);//Output: 10
           

條件語句 (Conditional Statements)

C# has the following conditional statement:

C#具有以下條件語句:

  • If

    如果

  • Else

    其他

  • If Else

    如果别的

  • Switch

    開關

If Statement

如果聲明

To execute a block of code if a specific condition is true.

如果特定條件為真,則執行代碼塊。

if (condition) {// block of code to be executed if the condition is true
}
           

Else Statement

其他聲明

To execute a block of code if a specific condition is false.

如果特定條件為假,則執行代碼塊。

if (condition) {// block of code to be executed if the condition is true
} else {// block of code to be executed if the condition is false
}
           

If Else Statement

否則聲明

To specify a new condition if the first condition is false.

如果第一個條件為假,則指定新條件。

if (condition1) {// block of code to be executed if condition1 is true
} else if (condition2) {// block of code to be executed if the condition1 is false and condition2 is true
} else {// block of code to be executed if the condition1 is false and condition2 is false
}
           

Switch

開關

The switch statement is used to select one of many code blocks to be executed.

switch語句用于選擇要執行的許多代碼塊之一。

switch(expression) {
  case x:// code block
    break;
  case y:// code block
    break;
  default:// code block
}
           

循環 (Loops)

Loops can execute a block of code as long as a specified condition is reached.

隻要達到指定條件,循環就可以執行代碼塊。

While Loop

While循環

The while loop loops through as long as a specific condition is true.

隻要滿足特定條件,while循環就會循環通過。

while (condition) {  // code block to be executed
}
           

For Loop

對于循環

When you know exactly how many times you want to loop through a block of code, you can use the for loop.

當您确切知道要周遊一段代碼的次數時,可以使用for循環。

for (statement 1; statement 2; statement 3) {// code block to be executed
 }
           

Statement 1 is executed before the execution of the code block.

語句1在執行代碼塊之前執行。

Statement 2 defines the condition for executing the code block.

語句2定義了執行代碼塊的條件。

Statement 3 is executed after the code block has been executed.

語句3在代碼塊執行後執行。

For Each Loop

對于每個循環

There is also a “for-each” loop that is used exclusively to loop through elements in an array:

還有一個“ for-each”循環,專用于循環周遊數組中的元素:

for (type variableName : arrayName) {// code block to be executed
}
           

結論 (Conclusion)

After this article, I hope you can write a simple C# code now and have a good idea of what C# is about.

讀完本文之後,希望您現在可以編寫一個簡單的C#代碼,并對C#的用途有一個很好的了解。

翻譯自: https://levelup.gitconnected.com/the-ultimate-c-guide-for-beginners-in-2020-bc4abc448f18