
No more than code.
// using 关键字用于在程序中包含 System 命名空间; 一个程序可以包含多个 using 语句
using System;
// 命名空间声明(Namespace declaration):一个 namespace DotNetCoreSample 命名空间包含了类 Program
namespace DotNetCoreSample
{
// class 声明:class 关键字用于声明一个类。类 Program 包含了程序使用的数据和方法声明。类一般包含多个方法。方法定义了类的行为。
class Program
{
// Main 方法:是所有 C# 程序的入口点。Main 方法说明当执行时 类将做什么动作
static void Main(string[] args)
{
// 控制台打印:WriteLine 是一个定义在 System 命名空间中的 Console 类的一个方法。该语句会在屏幕上显示消息 "Hello, World!"。
Console.WriteLine("Hello World!");
// 暂停当前程序:针对 VS.NET 用户的。这使得程序会等待一个按键的动作,防止程序从 Visual Studio.NET 启动时屏幕会快速运行并关闭。
Console.ReadKey();
}
}
}
using System;
namespace DotNetCoreSample
{
class Rectangle
{
// 成员变量
public double length;
public double width;
// public void Acceptdetails()
// {
// length = 4.5;
// width = 3.5;
// }
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
// length 和 width 被声明为 public,所以它们可以被函数 Main() 使用 Rectangle 类的实例 r 访问
r.length = 4.5;
r.width = 3.5;
// r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
成员变量 变量是类的属性或数据成员,用于存储数据。在上面的程序中,Rectangle 类有两个成员变量,名为 length 和 width。
成员函数 函数是一系列执行指定任务的语句。类的成员函数是在类内声明的。我们举例的类 Rectangle 包含了三个成员函数: AcceptDetails、GetArea 和 Display。
封装使用 访问修饰符 来实现。一个 访问修饰符 定义了一个类成员的范围和可见性。C# 支持的访问修饰符如下所示:
Protected internal 允许一个类将其成员变量和成员函数对同一应用程序内的子类以外的其他的类对象和函数进行隐藏。这也被用于实现继承
C# 提供了下列内置的类型转换方法:
序号 | 方法 | 描述 |
1 | ToBoolean | 如果可能的话,把类型转换为布尔型。 |
2 | ToByte | 把类型转换为字节类型。 |
3 | ToChar | 如果可能的话,把类型转换为单个 Unicode 字符类型。 |
4 | ToDateTime | 把类型(整数或字符串类型)转换为 日期-时间 结构。 |
5 | ToDecimal | 把浮点型或整数类型转换为十进制类型。 |
6 | ToDouble | 把类型转换为双精度浮点型。 |
7 | ToInt16 | 把类型转换为 16 位整数类型。 |
8 | ToInt32 | 把类型转换为 32 位整数类型。 |
9 | ToInt64 | 把类型转换为 64 位整数类型。 |
10 | ToSbyte | 把类型转换为有符号字节类型。 |
11 | ToSingle | 把类型转换为小浮点数类型。 |
12 | ToString | 把类型转换为字符串类型。 |
13 | ToType | 把类型转换为指定类型。 |
14 | ToUInt16 | 把类型转换为 16 位无符号整数类型。 |
15 | ToUInt32 | 把类型转换为 32 位无符号整数类型。 |
16 | ToUInt64 | 把类型转换为 64 位无符号整数类型。 |
C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。例如 Nullable< bool > 变量可以被赋值为 true 或 false 或 null。
声明一个 nullable 类型(可空类型)的语法:< data_type> ? <variable_name> = null;
如: int? num2 = 45;
Null 合并运算符( ?? )
Null 合并运算符用于定义可空类型和引用类型的默认值。如果第一个操作数的值为 null,则运算符返回第二个操作数的值,否则返回第一个操作数的值。
double? num1 = null;
double? num2 = 3.14157;
double num3;
num3 = num1 ?? 5.34;
Console.WriteLine("num3 的值: {0}", num3); // 5.34
num3 = num2 ?? 5.34;
Console.WriteLine("num3 的值: {0}", num3); // 3.14157
声明数组
datatype[] arrayName;
初始化数组
声明一个数组不会在内存中初始化数组。数组是一个引用类型,所以您需要使用 new 关键字来创建数组的实例。
double[] balance = new double[10];
数组赋值
double[] balance = new double[10]; balance[0] = 4500.0;
double[] balance = { 2340.0, 4523.69, 3421.0};
int [] marks = new int[5] { 99, 98, 92, 97, 95};
int [] marks = new int[] { 99, 98, 92, 97, 95};
int [] marks = new int[] { 99, 98, 92, 97, 95}; int[] score = marks;
launch.json中使用配置中的console属性:
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/DotNetCoreSample/bin/Debug/netcoreapp3.1/DotNetCoreSample.dll",
"args": [],
"cwd": "${workspaceFolder}/DotNetCoreSample",
"console": "integratedTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}