博主刚开始接触C#,本系列为学习记录,如有错误欢迎各位大佬指正!期待互相交流!
文章目录
- 一、C#语言介绍
-
- 1.1 C#注释方法
- [1.2 标准格式](#1.2 标准格式)
- 二、字符串相关
-
- [2.1 Console.WriteLine输出](#2.1 Console.WriteLine输出)
-
- [2.1.1 输出字符串](#2.1.1 输出字符串)
- [2.1.2 字符串内插](#2.1.2 字符串内插)
- [2.2 获取字符串长度](#2.2 获取字符串长度)
- [2.3 裁剪字符串中的空格](#2.3 裁剪字符串中的空格)
- [2.4 替换字符串](#2.4 替换字符串)
- [2.5 字符输出全部大(小)写](#2.5 字符输出全部大(小)写)
- [2.6 搜索字符串](#2.6 搜索字符串)
- [2.7 输出字符串加数字变量](#2.7 输出字符串加数字变量)
- 三、数据类型
- 四、控制语句
-
- [4.1 判断语句](#4.1 判断语句)
- [4.2 循环语句](#4.2 循环语句)
-
- [4.2.1 while循环](#4.2.1 while循环)
- [4.2.2 do...while循环](#4.2.2 do…while循环)
- [4.2.3 for循环](#4.2.3 for循环)
- 五、使用泛型列表类型管理数据集合
-
- [5.1 var关键字](#5.1 var关键字)
- [5.2 定义泛型列表](#5.2 定义泛型列表)
- [5.3 遍历List](#5.3 遍历List)
- [5.4 添加/删除元素](#5.4 添加/删除元素)
- [5.5 获取列表长度](#5.5 获取列表长度)
- [5.6 按索引引用各项](#5.6 按索引引用各项)
- [5.7 按索引添加/删除元素](#5.7 按索引添加/删除元素)
- [5.8 查找元素](#5.8 查找元素)
- [5.9 元素排序](#5.9 元素排序)
- [5.10 清空列表](#5.10 清空列表)
一、C#语言介绍
1.1 C#注释方法
C#注释方法与C语言相同,单行注释以 // 开头,多行注释以 /* 开头,以 */ 结尾。
同样的,C#每一句也以 ";" 结尾。
1.2 标准格式
C#语言的标注格式为
csharp
using System;
class Hello
{
static void Main()
{
// This line prints "Hello, World"
Console.WriteLine("Hello, World");
}
}
以下内容来源于C#交互式学习过程,C#交互式教程链接:Hello World - 交互式入门教程。
二、字符串相关
2.1 Console.WriteLine输出
2.1.1 输出字符串
可以使用Console.WriteLine
输出字符串,比如
csharp
Console.WriteLine("Hello World!");
或者自己定义一个字符串变量输出,比如
csharp
string aFriend = "Bill";
Console.WriteLine(aFriend);
2.1.2 字符串内插
可以使用"+"将定义的字符串变量与常量字符串组合输出,比如
csharp
string aFriend = "Bill";
Console.WriteLine("Hello " + aFriend);
也可以在字符串的左引号前添加 $,然后在大括号之间的字符串内包括变量,比如
csharp
string aFriend = "Bill";
Console.WriteLine($"Hello {aFriend}");
这两种方式的运行结果是一样的,都是
csharp
Hello Bill
2.2 获取字符串长度
可以使用Length
来获取字符串的长度,比如
csharp
string aFriend = "Bill";
Console.WriteLine($"Hello {aFriend.Length}");
输出结果为4
。
2.3 裁剪字符串中的空格
可以使用Trim
、TrimStart
和TrimEnd`来裁剪字符串中的空格,比如
csharp
string aFriend = " Bill ";
// 初始字符串,[]是为了方面观察裁剪结果
Console.WriteLine($"[{aFriend}]");
// 裁剪掉全部的空格
string trimaFriend = aFriend.Trim();
Console.WriteLine($"[{trimaFriend}]");
// 裁剪掉前面的空格
trimaFriend = aFriend.TrimStart();
Console.WriteLine($"[{trimaFriend}]");
// 裁剪掉后面的空格
trimaFriend = aFriend.TrimEnd();
Console.WriteLine($"[{trimaFriend}]");
输出结果为
csharp
[ Bill ]
[Bill]
[Bill ]
[ Bill]
2.4 替换字符串
可以使用Replace
来实先字符串的替换,Replace
需要输入两个参数,第一个是要替换的字符串,第二个是要替换的内容,Replace
会先搜索第一个字符串,之后将第一个字符串替换成第二个,比如
csharp
string sayHello = "Hello World!";
Console.WriteLine(sayHello);
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);
输出结果为
csharp
Hello World!
Greetings World!
2.5 字符输出全部大(小)写
csharp
ToUpper:全部大写;
ToLower:全部小写;
比如
csharp
string sayHello = "Hello World!";
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());
输出结果为
csharp
HELLO WORLD!
hello world!
2.6 搜索字符串
Contains
可确定字符串是否包含子字符串,如果包含会返回True
,如果不包含会返回False
,比如
csharp
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("greetings"));
输出结果是
csharp
True
False
2.7 输出字符串加数字变量
比如输出一个变量的值,前面需要加上字符串描述
csharp
int AdValue = 2335;
Console.WriteLine ($"AdValue is {AdValue}");
输出结果为
csharp
AdValue is 2335
三、数据类型
C#中也有和C语言相同的int
、float
、double
型变量,也可以使用decimal
来定义一个十进制变量,decimal
类型的范围较小,但精度高于double
,比如要定义一个decimal
型变量
csharp
decimal c = 1.0M;
数字中的M
后缀指明了常数应如何使用decimal
类型。 否则,编译器假定为double
类型。
四、控制语句
C#中的控制语句与C语言相似,也有if判断、for、while循环或者switch等,用法也基本相同。
4.1 判断语句
直接举例说明
csharp
int a = 5;
int b = 3;
int c = 4;
if ((a + b + c > 10) && (a == b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("And the first number is equal to the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("Or the first number is not equal to the second");
}
其中用到的一些比如"&&"、"||"和"=="也都和C语言相同。
4.2 循环语句
循环语句也是和C语言相同,有三种循环语句,直接举例说明
4.2.1 while循环
csharp
int counter = 0;
while (counter < 10)
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
}
4.2.2 do...while循环
csharp
int counter = 0;
do
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
} while (counter < 10);
4.2.3 for循环
csharp
for (int counter = 0; counter < 10; counter++)
{
Console.WriteLine($"Hello World! The counter is {counter}");
}
for循环也可以进行嵌套,这里就不在举例。
五、使用泛型列表类型管理数据集合
5.1 var关键字
C#中的var
关键字用于声明一个隐式类型的局部变量,使用var定义变量时,编译器会根据变量的初始化表达式推断出变量的具体类型,并将其隐式地设置为推断出的类型。
5.2 定义泛型列表
定义泛型列表的语句格式为
csharp
List<数据类型> LIstName = new List<数据类型>{Arr(元素)};
也可以用下面的方式来定义一个泛型列表
csharp
var ListName = new List<数据类型>{Arr(元素)};
5.3 遍历List
使用foreach
来遍历List中的元素,格式为
csharp
foreach (数据类型 item in ListName)
{
Console.WriteLine (item);
}
比如
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
foreach (var name in names)
{
Console.WriteLine(name);
}
5.4 添加/删除元素
使用Add
添加元素,使用Remove
删除元素,比如
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
foreach (var name in names)
{
Console.WriteLine(name);
}
names.Add("Maria");
names.Remove("Ana");
Console.WriteLine("<New List>");
foreach (var name in names)
{
Console.WriteLine(name);
}
5.5 获取列表长度
使用Count
获取列表长度
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
Console.WriteLine($"List Length is {names.Count}");
5.6 按索引引用各项
这个与C语言类似
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
Console.WriteLine(names[1]);
索引也是从0开始。
5.7 按索引添加/删除元素
使用Insert
来按照索引添加元素
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
names.Insert(1,"Maria");
foreach (var name in names)
{
Console.WriteLine(name);
}
使用RemoveAt
来按照索引删除元素
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
names.RemoveAt(1);
foreach (var name in names)
{
Console.WriteLine(name);
}
5.8 查找元素
使用IndexOf
查找元素,如果该元素不在列表内,返回-1
,如果元素在列表内返回元素索引
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
if (names.IndexOf("Ana") != -1)
{
Console.WriteLine (names.IndexOf("Ana"));
}
5.9 元素排序
使用Sort
对列表元素进行排序,如果元素是字符串则按照首字母顺序排序
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
names.Sort();
foreach (var name in names)
{
Console.WriteLine(name);
}
5.10 清空列表
csharp
List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
names.Clear();