CSharp新手必备代码
时间: 2023-11-12 15:51:25 浏览: 104
CSharp 代码入门
作为一个CSharp新手,以下是一些你可能会找到有用的常用代码:
1. Hello World程序:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
```
2. 变量和数据类型:
```csharp
int number = 10;
double decimalNumber = 3.14;
string text = "CSharp";
Console.WriteLine(number);
Console.WriteLine(decimalNumber);
Console.WriteLine(text);
```
3. 条件语句:
```csharp
int number = 10;
if (number > 5)
{
Console.WriteLine("Number is greater than 5");
}
else if (number < 5)
{
Console.WriteLine("Number is less than 5");
}
else
{
Console.WriteLine("Number is equal to 5");
}
```
4. 循环:
```csharp
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
int j = 0;
while (j < 5)
{
Console.WriteLine(j);
j++;
}
```
5. 函数:
```csharp
int Add(int a, int b)
{
return a + b;
}
int result = Add(3, 4);
Console.WriteLine(result);
```
这些代码片段可以帮助你开始学习CSharp编程。记得不断练习和探索,提高你的编程技能!
阅读全文