net 框架
dotnet --version # 版本信息
dotnet new console -o <project-name> # 创建一个新的控制台应用
dotnet run # 编译并运行一个C#程序
数据类型
值类型:基本类型(字符串除外),枚举,元组和结构
引用类型:类,字符串,接口,数组和委托
字符串
字符串可变性
- System.String:不可变的字符序列
- System.Text.StringBuilder:变的字符序列 ```csharp using System; char[] cdb = { ‘M’, ‘y’, ‘S’, ‘q’, ‘l’ }; string db = new string(cdb); // MySql
string lang = “C#”; // C# String ide = “NetBeans”; // NetBeans
```csharp
using System.Text;
string lang = "C#";
StringBuilder sb1 = new StringBuilder(lang); // C#
StringBuilder sb2 = new StringBuilder(); // Fields of glory
sb2.Append("Fields");
sb2.Append(" of ");
sb2.Append("glory");
sb.Remove(sb.Length-1, 1);
sb.Insert(3, ' ');
sb.Replace('M', 'm', 4, 1);
字符串是对象
对象必须具有一个类名,一个父类,并且还必须具有可以调用的方法或要访问的属性。
using System;
string lang = "Java";
string bclass = lang.GetType().Name; // String
string parclass = lang.GetType().BaseType.Name; // Object
var isEmpty = lang.Equals(String.Empty); // false
int len = lang.Length; // 4
连接字符串
"Return" + " of " + "the king.";
string.Concat(string.Concat("Return", " of ");
StringBuilder sb = new StringBuilder();
sb.Append("Return");
sb.Append(" of ");
sb.Append("the king.");
var items = new string[] { "C#", "Visual Basic", "Java", "Perl" };
string langs = string.Join(",", items); // C#,Visual Basic,Java,Perl
string[] langs2 = langs.Split(','); // [C#,Visual Basic,Java,Perl]
转义
using System;
string s1 = "deep \t forest"; // deep forest
不转义
using System;
Console.WriteLine(@"deep \t forest"); // deep \t forest
字符串格式化string.Format
using System;
int age = 34;
string name = "William";
string msg = string.Format("{0} is {1} years old.", name, age); // William is 34 years old.
字符串插值$
using System;
string name = "Peter";
int age = 34;
string msg = $"{name} is {age} years old"; // Peter is 34 years old
常用方法
using System;
string word = "Determination";
Console.WriteLine(word.Contains("e")); // 包含特定字符
Console.WriteLine(word.IndexOf("e")); // 返回字符串中字母的第一个索引
Console.WriteLine(word.LastIndexOf("i")); // 返回字符串中字母的最后一个索引
Console.WriteLine(word.ToUpper()); // 转换为大写
Console.WriteLine(word.ToLower()); // 转换为小写
string cloned = (string) str.Clone(); // ReferenceEquals(str, cloned) = true
string copied = string.Copy(str); // ReferenceEquals(str, cloned) = false
数组
int[] array = new int[] {2, 4, 5, 6, 7, 3, 2 }; // 初始化
int[] vals2 = vals[1..5]; // 数组切片,[4, 5, 6, 7, 3]
int[] vals3 = vals[..6]; // [2, 4, 5, 6, 7, 3, 2 ]
int[] vals4 = vals[3..]; // [6, 7, 3, 2]
for (int i=0; i < array.Length; i++) {} // 数组遍历
foreach (string planet in array) {}
names.GetValue(1);
names.SetValue("Beky", 1);
Array.Sort(names);
Array.Reverse(names);
Array.IndexOf(names, "Erzebeth");
Array.Copy(names, girls, names.Length);
Array.Clear(names, 0, 2);
字符
using System;
string word = "ZetCode";
char c = word[0]; // Z
元组
using System;
var words = ("sky", "blue", "rock", "fountain");
Console.WriteLine(words.Item1); // sky
Console.WriteLine(words.Item2); // blue
DateTime
using System;
DateTime now = DateTime.Now; // 12/5/2018 8:09:56 PM
DateTime now1 = DateTime.Now.ToShortDateString(); // 12/5/2018
DateTime now2 = DateTime.Now.ToShortTimeString(); // 8:09 PM
可空类型?
using System;
Nullable<bool> male = null;
int? age = null;
类型转换()
using System;
double b = 13.5;
float a = (float) b; // 将double值转换为float值
值转换Convert
using System;
// 将double值转换为bool值
Console.WriteLine(Convert.ToBoolean(0.3));
Console.WriteLine(Convert.ToBoolean(3));
Console.WriteLine(Convert.ToBoolean(0));
Console.WriteLine(Convert.ToBoolean(-1));
Console.WriteLine(Convert.ToInt32("452"));
Console.WriteLine(Convert.ToInt32(34.5));
// 将string转换为int值
Console.WriteLine(Int32.Parse("34"));
Console.WriteLine(Int32.Parse("-34"));
Console.WriteLine(Int32.Parse("+34"));
C# 运算符
类别 | 符号 |
---|---|
符号运算符 | + - |
算术 | + - * / % |
逻辑(布尔和按位) | & | ^ ! ~ && || true false |
字符串连接 | + |
递增,递减 | ++ — |
移位 | << >> |
关系 | == != < > <= >= |
辅助 | = += -= *= /= %= &= |= ^= ??= <<= >>= |
成员访问 | . ?. |
索引 | [] ?[] |
调用 | () |
三元 | ?: |
委托连接和删除 | + - |
对象创建 | new |
类型信息 | as is sizeof typeof |
异常控制 | checked unchecked |
间接地址 | * -> [] & |
Lambda | => |
控制台的值Console.ReadLine
using System;
Console.WriteLine("This is C#"); // 等同于System.Console.WriteLine("This is C#");
string name = Console.ReadLine(); // 从控制台读取一个值
数组定义List
using System.Collections.Generic;
var words = new List<string> { "stone", "rock", "falcon", "sky" };
解构丢弃_
using System;
var vals = (1, 2, 3, 4, 5, 6);
(int x, int y, int z, _, _, _) = vals;
生成指定范围内的数字序列Enumerable.Range
using System.Linq;
var vals = Enumerable.Range(1, 6); // [1,2,3,4,5,6]
随机数random
using System;
Random random = new Random();