1.环境搭建
因为不喜欢vscode的臃肿,所以决定自己搭建.NET环境
(.Net 框架应用程序是多平台的应用程序。框架的设计方式使它适用于下列各种语言:C#、C++、Visual Basic、Jscript、COBOL 等等。所有这些语言可以访问框架,彼此之间也可以互相交互。)
参考:https://blog.csdn.net/CHQC388/article/details/106397991
直接官网下载(https://dotnet.microsoft.com/zh-cn/download/dotnet )点击安装等结束就行,微软一贯的傻瓜式操作。
.NET使用的指令是dotnet
,默认安装目录也是C:\Program Files\dotnet
然后使用以下命令查看版本信息:
1 | dotnet --info |
安装这里有一个小问题:推荐使用的稳定版本是.NET6.0,但现行的大部分教程都是.NET3版本的,ctf最多也就遇见过.NET5.0,以下是.NET6.0和前版本的初始cs文件的差别:
.NET6.0:
1 | // See https://aka.ms/new-console-template for more information |
.NET5.0/.NET3.1:
1 | using System; |
虽然差距很大,但好事情是.NET是向下兼容的,所以理论上.NET6.0可以用前版本的写法(前者写法仅为.NET6.0的新特性)
但本着“要学就学最新的”原则,肯定要考虑到“版本共存”的问题
而微软在这方面的用户体验做的确实不错,不用改环境变量之类的东西
直接下载.NET6.0和.NET3.1的安装包,分别进行安装就行(.NET6.0的安装包自带.NET5.0)
然后C:\Program Files\dotnet\sdk
目录下就会有三个版本的文件夹
然后使用命令dotnet --list-sdks
就可以查到自己安装的版本
(2)新建项目:dotnet new console --name helloworld
1 | 默认新建的是.net6.0版本 |
C:\Users\User\Desktop\c#_code>dotnet new console -h
控制台应用 (C#)
作者: Microsoft
说明: 用于创建可在 Windows、Linux 和 macOS 上 .NET 上运行的命令行应用程序的项目
选项:
-f|–framework The target framework for the project.
net6.0 - Target net6.0
net5.0 - Target net5.0
netcoreapp3.1 - Target netcoreapp3.1
默认: net6.0
–langVersion Sets the LangVersion property in the created project file
text - Optional
–no-restore If specified, skips the automatic restore of the project on create.
bool - Optional
默认: false
要查看有关其他模板语言(F#, VB)的帮助,请使用 –language 选项:
dotnet new console -h –language F#
1 | 一目了然,切换版本用```-f```指令就好 |
–obj
|
–HelloCsharp.csproj
|
–Program.cs //主程序代码
1 | (4)构建项目 |
dotnet build
1 | 会自动创建```bin > Debug > net6.0```文件夹,下面就有可执行文件```HelloCsharp.exe``` |
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* 我的第一个 C# 程序*/
Console.WriteLine(“Hello World”);
}
}
}
1 | 程序的第一行 using System; - using 关键字用于在程序中包含 System 命名空间。 一个程序一般有多个 using 语句。 |
using System;
namespace RectangleApplication
{
class Rectangle
{
// 成员变量
double length;
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();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
1 | (1)using 关键字用于在程序中包含命名空间。一个程序可以包含多个 using 语句。 |
标识符必须以字母、下划线或 @ 开头,后面可以跟一系列的字母、数字( 0 - 9 )、下划线( _ )、@。
标识符中的第一个字符不能是数字。
标识符必须不包含任何嵌入的空格或符号,比如 ? - +! # % ^ & * ( ) [ ] { } . ; : “ ‘ / \。
标识符不能是 C# 关键字。除非它们有一个 @ 前缀。 例如,@if 是有效的标识符,但 if 不是,因为 if 是关键字。
标识符必须区分大小写。大写字母和小写字母被认为是不同的字母。
不能与C#的类库名称相同。
1 | (4)打印:(和c类似)```Console.WriteLine("Length: {0}", length);``` |
Console.WriteLine(str.GetType());
1 | (1)基本数据类型 |
类型 描述 范围 默认值
bool 布尔值 True 或 False False
byte 8 位无符号整数 0 到 255 0
char 16 位 Unicode 字符 U +0000 到 U +ffff ‘\0’
decimal 128 位精确的十进制值,28-29 有效位数 (-7.9 x 1028 到 7.9 x 1028) / 100 到 28 0.0M
double 64 位双精度浮点型 (+/-)5.0 x 10-324 到 (+/-)1.7 x 10308 0.0D
float 32 位单精度浮点型 -3.4 x 1038 到 + 3.4 x 1038 0.0F
int 32 位有符号整数类型 -2,147,483,648 到 2,147,483,647 0
long 64 位有符号整数类型 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 0L
sbyte 8 位有符号整数类型 -128 到 127 0
short 16 位有符号整数类型 -32,768 到 32,767 0
uint 32 位无符号整数类型 0 到 4,294,967,295 0
ulong 64 位无符号整数类型 0 到 18,446,744,073,709,551,615 0
ushort 16 位无符号整数类型 0 到 65,535 0
1 | (2)内置的引用类型:```object、dynamic 和 string``` |
dynamic
1 | 例如: |
dynamic d = 20;
1 | 动态类型与对象类型相似,但是对象类型变量的类型检查是在编译时发生的,而动态类型变量的类型检查是在运行时发生的。 |
指针类型(Pointer types)
1 |
|
using System;
namespace net3demotest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello World!”);
int num;
num=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(type(str));
}
}
}
1 | 转换成int格式: |
using System;
namespace net3demotest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello World!”);
int num;
num=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num);
}
}
}
1 |
|
double[] balance = new double[10];
balance[0] = 4500.0;
1 | ``` |
1 | int [] marks = new int[5] { 99, 98, 92, 97, 95}; |
1 | int [] marks = new int[] { 99, 98, 92, 97, 95}; |
也可以赋值一个数组变量到另一个目标数组变量中。在这种情况下,目标和源会指向相同的内存位置:
1 | int [] marks = new int[] { 99, 98, 92, 97, 95}; |
foreach:
1 | using System; |
类的内容略过
8.反射
因为这是“加一层”的基础,所以强化一下
优点:
1、反射提高了程序的灵活性和扩展性。
2、降低耦合性,提高自适应能力。
3、它允许程序创建和控制任何类的对象,无需提前硬编码目标类。
缺点:
1、性能问题:使用反射基本上是一种解释操作,用于字段和方法接入时要远慢于直接代码。因此反射机制主要应用在对灵活性和拓展性要求很高的系统框架上,普通程序不建议使用。
2、使用反射会模糊程序内部逻辑;程序员希望在源代码中看到程序的逻辑,反射却绕过了源代码的技术,因而会带来维护的问题,反射代码比相应的直接代码更复杂。
1 | using System; |
结果:
1 | Length: 4.5 |