基础
相关命令行工具
- dotnet --version
查看 .NET Core SDK版本 - dotnet new console
创建控制台应用 - dotnet run
运行当前目录源代码
C#关键字
100多个关键字
using、namespace、class、static、int、string、double
使用变量及类型、数组
命名和赋值
- 局部变量、私有变量
采用小驼峰 - 类型、非私有变量
采用大驼峰 - 存储文本的转义字符
采用@符号作为前缀,禁用转义字符
string filePath=@"C:\televisions\sony\etc.txt";
储存数字
1.#7以及更高支持下划线_作为数字分隔符,例如10000可以写成10_000
2.float、double采用单双精度,会有精度的缺失,不可用==比较是否相等
3.decimal适用于货币、制图等高精度
4.double的其他方法
- double.NaN 表示不是数字
- double.Epsilon 表示可以存储在double的最小正数
- double.IsInfinity() 表示是负无穷还是正无穷
5.bool true或者false
int decimalNotation=2_000_000;
int binaryNotation=0b_0001_1110_1000_0100_1000_0000;
int hexadecimalNotation=0x_001E_8480;
Console.WriteLine($"{decimalNotation==binaryNotation}");
Console.WriteLine($"{decimalNotation==hexadecimalNotation}");
object类型
可存储任何类型,但是是以混淆代码的可维护性和性能为前提
object height=1.88;
object name="Amir";
Console.WriteLine($"{name} is {height} metres tall.");
int length1=name.Length; //无法编译 object类型没有此方法需要强转
int length2=((string)name).Length;
Console.WriteLine($"{name} is {length2} metres tall.");
动态存储类型dynamic关键字
C#4.0引入,其存储的变量可以在没有显式进行强制转换的情况下调用成员。
其在编译时无法检查类型,CLR在运行时检查,可能会跑出异常
生成对象var和new
- var关键字声明局部变量,编译器将在操作赋值之后推断其类型
1.var关键字很方便
2.只在类型明显可以辨别时使用,否则语意不清 - C#9.0引入新语法,面向对象类型的new,不用重复写出类型
XmlDocument xml=new();
XmlDocument xml1=new XmlDocument();
获取类型的默认值
可以使用default()方法确定类型的默认值
基本类型都有默认值,引用类型的默认值为null,注意为null时,控制台不打印null,而是打印空白
Console.WriteLine(default(int));
Console.WriteLine(default(double));
Console.WriteLine(default(float));
Console.WriteLine(default(decimal));
Console.WriteLine(default(bool));
Console.WriteLine(default(byte));
Console.WriteLine(default(string));
Console.WriteLine(default(string)==null);
Console.WriteLine(default( List<string>));
Console.WriteLine(default( List<string>)==null);
数组
下标从0开始
string[] names = new String[4];
names[0]="Kate";
names[1]="Jack";
names[2]="Rebecca";
names[3]="Tom";
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(i);
}
处理空值null
在C#8.0中,可以配置引用类型不可为null,不再允许null值
- 在项目文件中加入Nullable属性为enable
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
- 单个文件启用
文件头顶部增加
#nullable disable - 单个文件禁用
#nullable enable
声明不可为空的变量和参数
在类型声明后加上?
class Address
{
public string? Building = string.Empty;
public string Street = string.Empty;
public string City = string.Empty;
public string Region = string.Empty;
}
判空检查null
- 空条件运算符
如果为null,C#可能抛出NullReferenceException可以使用三元运算符 ==
? : 或者空条件运算符 ?.
A=B==null?B.property:false
等价于
A==B?.property - 空合并操作符??.
如果B为null则A=C,否则A=B
A==B??.C
using命名空间
类似java、python的import
using static System.Console;
用以简化代码
控制台
##输出
Write、WriteLine方法
- 使用编号位置参数进行格式化
int numberOfApples=12;
decimal pricePerApple=0.35M;
Console.WriteLine(format:"{0} apples costs {1:C}",arg0: numberOfApples,arg1: pricePerApple*numberOfApples);
- 使用内插字符串
Console.WriteLine($"{numberOfApples} apples costs {numberOfApples*pricePerApple:C}");
输入
- ReadLine方法从用户获取文本
Console.Write("Type you first name and press ENTER:");
string? firstname = Console.ReadLine();
Console.Write("Type you age and press ENTER:");
string? age = Console.ReadLine();
Console.WriteLine($"Hello {firstname},you look good for {age}");
- 获取用户的重要输入(对应键盘代号)
Write("Press any key combination: ");
ConsoleKeyInfo key=ReadKey();
WriteLine();
WriteLine($"Key:{key.Key},Char:{key.KeyChar},Modifiers:{key.Modifiers}");
- 获取启动参数
sting[] args
命令行参数由空格分离,如果需要空格则需要包裹在单引号或者双引号中
using static System.Console;
using System;
namespace Arguments
{
class MyClass
{
static void Main(string[] args)
{
string arg = null;
foreach (string s in args)
{
arg += s;
}
WriteLine($"There are {args.Length} arguments.{arg}");
}
}
}
Comments | 0 条评论