C#学习笔记

入门

方法、类

Console是一种代表控制台窗口的类型。
WriteLine是一种Console将一行文本打印到该文本控制台的方法。

Console.WriteLine("Hello World!");

Hello World!

变量、字符串

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

Bill

变量常量 字符串构建字符串:+

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

Maira
Hello Maira

字符串插值:{}

在{…}字符之间放置一个变量,以告诉 C# 用变量的值替换该文本。这称为字符串插值。
如果$在字符串的左引号之前添加 a ,则可以aFriend在大括号之间的字符串内包含变量

string firstFriend = "Maria";
string secondFriend = "Sage";
Console.WriteLine($"My friends are {firstFriend} and {secondFriend}");

My friends are Maria and Sage

字符串属性:Length

字符串中含有?个字母:

string firstFriend = "Maria";
string secondFriend = "Sage";
Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters.");
Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters.");

The name Maria has 5 letters.
The name Sage has 4 letters.

修剪字符串中的空格


/*修剪字符串中的空格:TrimStart-剪前面空格;TrimEnd-剪后面空格;Trim-前后一起剪除*/
string greeting = "      Hello World!       ";
/*Console.WriteLine($"[{greeting}]");/*[      Hello World!       ]*/
string trimmedGreeting = greeting.TrimStart();
/*Console.WriteLine($"[{trimmedGreeting}]");/*[Hello World!       ]*/
/*trimmedGreeting = greeting.TrimEnd();/*[      Hello World!]*/
/*Console.WriteLine($"[{trimmedGreeting}]");*/
trimmedGreeting = greeting.Trim();/*[Hello World!]*/
Console.WriteLine($"[{trimmedGreeting}]");

整数、小数

整数类:int

int类型表示一个整数,正或负的整数。
可用加减乘除对其进行运算。

int a = 18;
int b = 6;
int c,d,e,f;
c = a + b;
d = a - b;
e = a * b;
f = a / b;
Console.WriteLine($"c={c},d={d},e={e},f={f}");

c=24,d=12,e=108,f=3

运算顺序

有括号优先括号内,没括号优先乘除。然后从左往右依次加减。

int a = 5;
int b = 4;
int c = 2;
int d = (a + b) - 6 * c + (12 * 4) / 3 + 12;
Console.WriteLine(d);

25

整数运算结果永远是整数

11除以3,结果不是整数。然,int类下,结果为整数。

int a = 7;
int b = 4;
int c = 3;
int d = (a + b) / c;
Console.WriteLine(d);

3

余数:%
int a = 7;
int b = 4;
int c = 3;
int d = (a + b) / c;
int e = (a + b) % c;
Console.WriteLine($"quotient: {d}");/*商*/
Console.WriteLine($"remainder: {e}");/*余数*/

quotient: 3
remainder: 2

int类的最大、最小限制
int max = int.MaxValue;
int min = int.MinValue;
Console.WriteLine($"The range of integers is {min} to {max}");

The range of integers is -2147483648 to 2147483647

如果计算产生的值超出这些限制,则存在下溢或上溢情况。答案似乎从一个限制到另一个限制。

int max = int.MaxValue;
int min = int.MinValue;
Console.WriteLine($"The range of integers is {min} to {max}");
int what = max + 2;
Console.WriteLine($"An example of overflow: {what}");/*上溢*/

The range of integers is -2147483648 to 2147483647
An example of overflow: -2147483647

浮点数:double、float、decimal

浮点数数值:整数或定点数(即尾数)。
float:单精度类型。最多可以精确到小数点为7位,占用内存为4字节,占用内存的消耗速度为double的一半
double:双精度类型。最多可以精确到小数点为16位,占用内存为8字节,占用内存的消耗速度为float的两倍
一般来说,CPU处理单精度浮点数的速度比处理双精度浮点数快。

单精度:float

float类型的变量赋值:整数、小数+f

float a = 19.5f;
float b = 23;
float c = 8;
float d = (a + b) / c;
Console.WriteLine(d);

5.3125

单精度的最大、最小限制(范围)
float max = float.MaxValue;
float min = float.MinValue;
Console.WriteLine($"The range of float is {min} to {max}");

The range of float is -3.402823E+38 to 3.402823E+38

双精度:double

double类型的变量赋值:整数、小数

double a = 19.0;
double b = 23;
double c = 8;
double d = (a + b) / c;
Console.WriteLine(d);

5.25

双精度的最大、最小限制(范围)
double max = double.MaxValue;
double min = double.MinValue;
Console.WriteLine($"The range of double is {min} to {max}");

The range of double is -1.79769313486232E+308 to 1.79769313486232E+308
E左边的数字是有效数。右边的数字是指数,是 10 的幂:
-1.79769313486232 x 10 的 308次方到 1.79769313486232 x 10的 308次方

精确小数位验证
取值不准确的特殊情况
float third = 1.0f / 3.0f;
Console.WriteLine(third);

0.3333333

double third = 1.0 / 3.0;/*除不尽的数*/
Console.WriteLine(third);

0.333333333333333
0.333…这个写法叫十进制写法,要求每个位置上表达一个10进制数。10不能被3整除,于是每位只能取到、不够10/3但最接近的数字3,然后通过下一位来补上不足1/10的余数。
但因为总归10无法被3整除,每次的余数又都是0.333…,就只好无限重复这个补值的过程。
以极限的观点来看是能描述1/3这个数值的。

混合数据类型运算
int a = 1;
float b = 3.5f;
double c = 7.66;
double d = (a + b) / c;/*最后运算的数值类型只能用 double,不能用int、float*/
Console.WriteLine(d);

0.587467362924282

高精度:decimal(M)
double a = 1.0;
double b = 3.0;
Console.WriteLine(a / b);

decimal c = 1.0M;/*M数字的后缀是指示常量应使用decimal类型的方式。否则,编译器采用该double类型。*/
decimal d = 3.0M;
Console.WriteLine(c / d);

0.333333333333333
0.3333333333333333333333333333
字母M被选为double和decimal关键字之间视觉上最明显的字母。

高精度的最大、最小限制(范围)
decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
Console.WriteLine($"The range of the decimal type is {min} to {max}");

The range of the decimal type is -79228162514264337593543950335 to 79228162514264337593543950335

decimal、double、decimal对比
NameCTS TypeDe scriptionSignificant FiguresRange (approximate)
floatSystem Single32-bit single-precision floating point7±1.5 × 10?45 to ±3.4 × 1038
doubleSystem Double64-bit double-precision floating point15/16±5.0 × 10 ?324 to ±1.7 × 10308
decimalSystem Decimal128-bit high precision decimal notation28±1.0 × 10?28 to ±7.9 × 1028
圆的面积、π

请编写代码来计算半径为 2.50 厘米的圆的面积。圆的面积是半径的平方乘以 PI。
Tips:.NET 包含一个用于 PI 的常量Math.PI。Math.PI与System.Math命名空间中声明的所有常量一样,是一个double值。

double r = 2.5;/*半径radius :cm*/
double S = r*r*Math.PI;/*area——面积*/
Console.WriteLine($"圆的面积为: {S}平方厘米");

圆的面积为: 19.6349540849362平方厘米

分支、循环

if、else分支判断

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

The answer is not greater than 10
Or the first number is not equal to the second

while循环

下述两个代码块输出的结果相同
A:while
B:do……while

int counter = 0;/*counter变量*/
while (counter < 4)
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;/*增量运算符,正循环*/
}
int counter = 0;
do
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
} while (counter < 4);

Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3

for循环

第一部分是for 初始化器:int counter = 0;声明它counter是循环变量,并将其初始值设置为0。
中间部分是for条件:counter < 10声明for只要counter的值小于10 ,这个循环就继续执行。
最后一部分是for 迭代器:counter++指定在执行for语句后面的块后如何修改循环变量。在这里,它指定counter每次块执行时应增加 1。

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

Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3

嵌套循环

while,do或for可被嵌套在另一个循环内使用,与内部循环组合成矩阵。
对于内循环的每次完整运行,外循环都会增加一次

for (int row = 1; row < 4; row++)/*生成行||外循环*/
{
  for (char column = 'a'; column < 'd'; column++)/*生成列||内循环*/
  {
    Console.WriteLine($"The cell is ({row}, {column})");
  }
}

The cell is (1, a)内循环运行1次
The cell is (1, b)内循环运行2次
The cell is (1, c)内循环运行3次
The cell is (2, a)外循环+1
The cell is (2, b)
The cell is (2, c)
The cell is (3, a)
The cell is (3, b)
The cell is (3, c)

for (int row = 1; row < 4; row++)
{
  for (char column = 'a'; column < 'd'; column++)
  {
    Console.WriteLine($"The cell is ({column}, {row})");
  }
}

The cell is (a, 1)
The cell is (b, 1)
The cell is (c, 1)
The cell is (a, 2)
The cell is (b, 2)
The cell is (c, 2)
The cell is (a, 3)
The cell is (b, 3)
The cell is (c, 3)

for (char column = 'a'; column < 'd'; column++)/*外循环*/
{
  for (int row = 1; row < 4; row++)/*内循环*/
  {
    Console.WriteLine($"The cell is ({column}, {row})");
  }
}

The cell is (a, 1)
The cell is (a, 2)
The cell is (a, 3)
The cell is (b, 1)
The cell is (b, 2)
The cell is (b, 3)
The cell is (c, 1)
The cell is (c, 2)
The cell is (c, 3)

1到20中能被3整数的总和(for、if嵌套)

/*1、整数1到20 √*/
/*2、被3整除 √*/
/*3、这些数的总和*/
int sum=0;
for (int a = 1 ; a < 21 ;a++)
{
    if(a%3==0)
    {
        sum += a;
        //标准答案:sum=sum+a;
        //+= 是简写,a += 1就是a = a+1
    }
}
Console.Writeline($"1到20中能被3整数的总和为 {sum}");/*要放在循环外!*/

1到20中能被3整数的总和为 63

列表类型管理数据集合

创建列表

调用String.ToUpper方法:
{name.ToUpper()}用每个名称替换 ,并转换为大写字母

var names = new List<string> { "Kun", "Ana", "Felipe" };
foreach (var name in names)
{
  Console.WriteLine($"Hello {name.ToUpper()}!");
}

Hello KUN!
Hello ANA!
Hello FELIPE!

列表操作(增、删、查、排序)

//初始
var names = new List<string> { "Kun","Ana", "Felipe" };/*初始-[0]、[删]、[1]*/
//增删
names.Add("Maria");/*增-[2]*/
names.Add("Bill");/*增-[3]*/
names.Remove("Ana");/*删*/
//打印并小写转大写
Console.WriteLine($"列表:");
foreach (var name in names)/*列表*/
{
    Console.WriteLine($" {name.ToUpper()}");/*小写转大写*/
}
//读表、并检查列表长度
Console.WriteLine( );
Console.WriteLine($"读表并检查列表长度:");
Console.WriteLine($"My name is {names[0]}.");/*读*/
Console.WriteLine($"I've added {names[2]} and {names[3]} to the list.");/*读*/
Console.WriteLine($"The list has {names.Count} people in it.");/*Count属性检查列表长度*/
//索引所在位置
//用"Kun", "Felipe","Maria","Bill"查看结果
//用"Ana"或其他查看结果
Console.WriteLine( );
Console.WriteLine($"列表数据索引:");
var index = names.IndexOf("Bill");/*任意数据*/
//变量index不等于-1时才输出
if (index != -1)/*index的值不等于-1——即表中有此数据*/
  Console.WriteLine($"The name {names[index]} is at index {index}");/*读的数据;返回索引*/
//变量notFound可以是任意值
var notFound = names.IndexOf("Ana");/*任意数据*/
Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");/*索引为空时返回-1*/
//排序
Console.WriteLine( );/*换行*/
Console.WriteLine($"按首字母排序:");
names.Sort();/*排序*/
foreach (var name in names)
{
  Console.WriteLine($"{name}");
}

列表:
KUN
FELIPE
MARIA
BILL

读表并检查列表长度:
My name is Kun.
I’ve added Maria and Bill to the list.
The list has 4 people in it.

列表数据索引:
The name Bill is at index 3
When an item is not found, IndexOf returns -1

按首字母排序:
Bill
Felipe
Kun
Maria

其他类型列表:斐波那契数列

fibonacciNumbers斐波那契数列又称黄金分割数列指的是这样一个数列:
0、1、1、2、3、5、8、13、21、34、……
这个数列从第3项开始,每一项都等于前两项之和。

var fibonacciNumbers = new List<int> {1, 1};/*第一个数、第二个数*/
var previous = fibonacciNumbers[fibonacciNumbers.Count - 1];/*第一项*/
var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2];/*第二项*/

fibonacciNumbers.Add(previous + previous2);/*列表添加:两项之和*/

foreach(var item in fibonacciNumbers)/*调用fibonacciNumbers的值生成列表*/
    Console.WriteLine(item);

1
1
2

生成序列中前20个数字的斐波那契数列,并算出他们的和
var fibonacciNumbers = new List<int> {1, 1};/*和基本式一致*/

while (fibonacciNumbers.Count < 20)/*生成序列中的前20个数字*/
{
    var previous = fibonacciNumbers[fibonacciNumbers.Count - 1];/*和基本式一致*/
    var previous2 = fibonacciNumbers[fibonacciNumbers.Count - 2];/*和基本式一致*/

    fibonacciNumbers.Add(previous + previous2);/*和基本式一致*/
}
foreach(var item in fibonacciNumbers)/*和基本式一致*/
    Console.WriteLine(item);/*和基本式一致*/

1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值