C#学习笔记(一)了解C#

博主刚开始接触C#,本系列为学习记录,如有错误欢迎各位大佬指正!期待互相交流!

一、C#语言介绍

1.1 C#注释方法

  C#注释方法与C语言相同,单行注释以 // 开头,多行注释以 /* 开头,以 */ 结尾。

  同样的,C#每一句也以 “;” 结尾。

1.2 标准格式

  C#语言的标注格式为

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输出字符串,比如

Console.WriteLine("Hello World!");

或者自己定义一个字符串变量输出,比如

string aFriend = "Bill";
Console.WriteLine(aFriend);

2.1.2 字符串内插

可以使用“+”将定义的字符串变量与常量字符串组合输出,比如

string aFriend = "Bill";
Console.WriteLine("Hello " + aFriend);

也可以在字符串的左引号前添加 $,然后在大括号之间的字符串内包括变量,比如

string aFriend = "Bill";
Console.WriteLine($"Hello {aFriend}");

这两种方式的运行结果是一样的,都是

Hello Bill

2.2 获取字符串长度

可以使用Length来获取字符串的长度,比如

string aFriend = "Bill";
Console.WriteLine($"Hello {aFriend.Length}");

输出结果为4

2.3 裁剪字符串中的空格

可以使用TrimTrimStart和TrimEnd`来裁剪字符串中的空格,比如

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}]");

输出结果为

[  Bill  ]
[Bill]
[Bill  ]
[  Bill]

2.4 替换字符串

可以使用Replace来实先字符串的替换,Replace需要输入两个参数,第一个是要替换的字符串,第二个是要替换的内容,Replace会先搜索第一个字符串,之后将第一个字符串替换成第二个,比如

string sayHello = "Hello World!";
Console.WriteLine(sayHello);
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);

输出结果为

Hello World!
Greetings World!

2.5 字符输出全部大(小)写

ToUpper:全部大写;
ToLower:全部小写;

比如

string sayHello = "Hello World!";

Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());

输出结果为

HELLO WORLD!
hello world!

2.6 搜索字符串

Contains可确定字符串是否包含子字符串,如果包含会返回True,如果不包含会返回False,比如

string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("greetings"));

输出结果是

True
False

2.7 输出字符串加数字变量

比如输出一个变量的值,前面需要加上字符串描述

int AdValue = 2335;
Console.WriteLine ($"AdValue is {AdValue}");

输出结果为

AdValue is 2335

三、数据类型

C#中也有和C语言相同的intfloatdouble型变量,也可以使用decimal来定义一个十进制变量,decimal类型的范围较小,但精度高于double,比如要定义一个decimal型变量

decimal c = 1.0M;

数字中的M后缀指明了常数应如何使用decimal类型。 否则,编译器假定为double类型。

四、控制语句

C#中的控制语句与C语言相似,也有if判断、for、while循环或者switch等,用法也基本相同。

4.1 判断语句

直接举例说明

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循环

int counter = 0;
while (counter < 10)
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
}

4.2.2 do…while循环

int counter = 0;
do
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
} while (counter < 10);

4.2.3 for循环

for (int counter = 0; counter < 10; counter++)
{
  Console.WriteLine($"Hello World! The counter is {counter}");
}

for循环也可以进行嵌套,这里就不在举例。

五、使用泛型列表类型管理数据集合

5.1 var关键字

C#中的var关键字用于声明一个隐式类型的局部变量,使用var定义变量时,编译器会根据变量的初始化表达式推断出变量的具体类型,并将其隐式地设置为推断出的类型。

5.2 定义泛型列表

定义泛型列表的语句格式为

List<数据类型> LIstName = new List<数据类型>{Arr(元素)};

也可以用下面的方式来定义一个泛型列表

var ListName = new List<数据类型>{Arr(元素)};

5.3 遍历List

使用foreach来遍历List中的元素,格式为

foreach (数据类型 item in ListName)
{
	Console.WriteLine (item);
}

比如

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
foreach (var name in names)
{
  Console.WriteLine(name);
}

5.4 添加/删除元素

使用Add添加元素,使用Remove删除元素,比如

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获取列表长度

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
Console.WriteLine($"List Length is {names.Count}");

5.6 按索引引用各项

这个与C语言类似

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };
Console.WriteLine(names[1]);

索引也是从0开始。

5.7 按索引添加/删除元素

使用Insert来按照索引添加元素

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };

names.Insert(1,"Maria");

foreach (var name in names)
{
  Console.WriteLine(name);
}

使用RemoveAt来按照索引删除元素

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };

names.RemoveAt(1);

foreach (var name in names)
{
  Console.WriteLine(name);
}

5.8 查找元素

使用IndexOf查找元素,如果该元素不在列表内,返回-1,如果元素在列表内返回元素索引

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };

if (names.IndexOf("Ana") != -1)
{
    Console.WriteLine (names.IndexOf("Ana"));
}

5.9 元素排序

使用Sort对列表元素进行排序,如果元素是字符串则按照首字母顺序排序

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };

names.Sort();

foreach (var name in names)
{
  Console.WriteLine(name);
}

5.10 清空列表

List<string> names = new List<string> { "Bill", "Ana", "Felipe" };

names.Clear();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二土电子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值