logo
💻 编程语言

C#

C# Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 编程语言🧭 Markdown 速查🏷️ 2 个标签
#csharp#dotnet
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Hello.cs
CS
滚动查看更多
class Hello {
  // main method
  static void Main(string[] args)
  {
    // Output: Hello, world!
    Console.WriteLine("Hello, world!");
  }
}

Creates a project directory for new console application

CS
滚动查看更多
$ dotnet new console

Lists all the applications templates

CS
滚动查看更多
$ dotnet new list

Compiling and running (make sure you are in the project directory)

SHELL
滚动查看更多
$ dotnet run
Hello, world!
Variables
CS
滚动查看更多
int intNum = 9;
long longNum = 9999999;
float floatNum = 9.99F;
double doubleNum = 99.999;
decimal decimalNum = 99.9999M;
char letter = 'D';
bool @bool = true;
string site = "cheatsheets.zip";

var num = 999;
var str = "999";
var bo = false;
Primitive Data Types
Data TypeSizeRange
int4 bytes-2^31^ ^to^ 2^31^-1
long8 bytes-2^63^ ^to^ 2^63^-1
float4 bytes6 ^to^ 7 decimal digits
double8 bytes15 decimal digits
decimal16 bytes28 ^to^ 29 decimal digits
char2 bytes0 ^to^ 65535
bool1 bittrue / false
string2 bytes per charN/A

{.show-header}

Comments
CS
滚动查看更多
// Single-line comment

/* Multi-line
   comment */

// TODO: Adds comment to a task list in Visual Studio

/// Single-line comment used for documentation

/** Multi-line comment
    used for documentation **/

Strings
CS
滚动查看更多
string first = "John";
string last = "Doe";

// string concatenation
string name = first + " " + last;
Console.WriteLine(name); // => John Doe

See: Strings

User Input
CS
滚动查看更多
Console.WriteLine("Enter number:");
if(int.TryParse(Console.ReadLine(),out int input))
{
  // Input validated
  Console.WriteLine($"You entered {input}");
}
Conditionals
CS
滚动查看更多
int j = 10;

if (j == 10) {
  Console.WriteLine("I get printed");
} else if (j > 10) {
  Console.WriteLine("I don't");
} else {
  Console.WriteLine("I also don't");
}
Arrays
CS
滚动查看更多
char[] chars = new char[10];
chars[0] = 'a';
chars[1] = 'b';

string[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
bool[] answers = {true, false};
Loops
CS
滚动查看更多
int[] numbers = {1, 2, 3, 4, 5};

for(int i = 0; i < numbers.Length; i++) {
  Console.WriteLine(numbers[i]);
}

CS
滚动查看更多
foreach(int num in numbers) {
  Console.WriteLine(num);
}

C# Strings

String concatenation
CS
滚动查看更多
string first = "John";
string last = "Doe";

string name = first + " " + last;
Console.WriteLine(name); // => John Doe
String interpolation
CS
滚动查看更多
string first = "John";
string last = "Doe";

string name = $"{first} {last}";
Console.WriteLine(name); // => John Doe
String Members
MemberDescription
LengthA property that returns the length of the string.
Compare()A static method that compares two strings.
Contains()Determines if the string contains a specific substring.
Equals()Determines if the two strings have the same character data.
Format()Formats a string via the {0} notation and by using other primitives.
Trim()Removes all instances of specific characters from trailing and leading characters. Defaults to removing leading and trailing spaces.
Split()Removes the provided character and creates an array out of the remaining characters on either side.

{.show-header}

Verbatim strings
CS
滚动查看更多
string longString = @"I can type any characters in here !#@$%^&*()__+ '' \n \t except double quotes and I will be taken literally. I even work with multiple lines.";
Member Example
CS
滚动查看更多
// Using property of System.String
string lengthOfString = "How long?";
lengthOfString.Length           // => 9

// Using methods of System.String
lengthOfString.Contains("How"); // => true

Misc

General .NET Terms
TermDefinition
RuntimeA collection of services that are required to execute a given compiled unit of code.
Common Language Runtime (CLR)Primarily locates, loads, and managed .NET objects. The CLR also handles memory management, application hosting, coordination of threads, performing security checks, and other low-level details.
Managed codeCode that compiles and runs on .NET runtime. C#/F#/VB are examples.
Unmanaged codeCode that compiles straight to machine code and cannot be directly hosted by the .NET runtime. Contains no free memory management, garbage collection, etc. DLLs created from C/C++ are examples.

{.show-header}

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572