admin管理员组

文章数量:1544579

@HX

记C#学习之路

回首四年前,高中毕业步入大学校园,那时候的青涩贪玩,四年大学生活浪浪荡荡,到现在踏入职场,走进新的征程。趁着年轻抓紧学习,
由java(入门级别)到应公司要求改行做C#,从了解到深入或许还需要一段时间。C#嘛微软的东西所以还是去微软的官网进行学习比较靠谱,SO决定将自己的学习心得以博文的形式记录下来。

字符串内插

如果在字符串的左引号前添加$,则可以在大括号之间的字符串内包含变量,如aFriend

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

不仅限于单个变量

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

Length是字符串属性,可以返回字符串中字符数

Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters.");

字符串去空格

控制字符串的方法返回的是新字符串对象,而不是就地进行修改

//打印原始状态首尾添加[]
string greeting ="       Hello World      ";
Console.WriteLine($"[{greeting}]");
//首去空格
string trimmedGreeting = greeting.TrimStart();
Console.WriteLine($"[{trimmedGreeting}]");
//尾去空格
trimmedGreeting =greeting.TrimEnd();
Console.WriteLine($"[{trimmedGreeting}]");
//全部去空格
trimmedGreeting = greeting.Trim();
Console.WriteLine($"[{trimmedGreeting}]");

Replace()方法两个参数,第一个参数是要搜索的文本,第二个参数是替换后的文本

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

将字符串全部大写或小写

Console.WriteLine(sayHello.toUpper());
Console.WriteLine(sayHello.toLower());

Contains查找字符串中的文本,返回布尔值,分别为True,False

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

搜索字符串开头或结尾的子字符串StartWith()和EndsWith()

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

整数运算精度和限值

int max =int.MaxValue;
int min =int.MinValue;
Console.WriteLine($"The range of integers is {min} to {max}");

双精度浮点数(存储值的二进制位数是单精度的两倍)

decimal&&double类型比较

double a = 1.0;
double b = 3.0;
Console.WriteLine(a / b);

decimal c = 1.0M;
decimal d = 3.0M;

Console.WriteLine(c / d);
//十进制精度更高
//0.333333333333333
//0.3333333333333333333333333333

圆面积公式计算

// using System.Math;
//PI为常数,相当于Π
double r = 2.50;
double PI = Math.PI;
double s=PI * r * r;
Console.WriteLine($"{s}");

循环 while&&do while

int counter = 0;
while(counter<10)
{
    Console.WriteLine($"循环输出counter:{counter}");
    counter++;
}

do 
{
    Console.WriteLine($"循环输出counter:{counter}");
    counter++;
}while(counter<10);

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

创建列表

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

修改列表中内容

var names = new List<string> {"<name>","Ana","Felipe"};
//向列表中添加
names.Add("Maria");
names.Add("Bill");
//删除列表中元素
names.Remove("Ana");
foreach(var name in names)
{
    Console.WriteLine($"Hello {name.ToUpper()}");
}
//索引访问具体项
Console.WriteLine($"第二个元素是 {names[1]}");
//统计项数
Console.WriteLine($"一共多少项 {names.Count}");

搜索列表并进行排序

搜索
//IndexOf()返回索引值 返回-1则无此项
var index = names.IndexOf("Felipe");
if(index != -1)
{
    Console.WriteLine($"The name {names[index]} is at index {index}");
}

var notFound = names.IndexOf("Not Found");
Console.WriteLine($"when an item is not found,IndexOf returns {notFound}");
排序
//按正常顺序 是字符串按首字母顺序
//既有字符串又有中文 中文放在字符串后
name.Sort();

斐波那契数列

var number = new List<int> {1,1};
var previous = number [number.Count -1];

var previous2 =number [number.Count -2];

number.Add(previous + previous2);
foreach(var item in number)
{
    Console.WriteLine($"{item}");
}
生成20个项
var fibonacciNumbers = new List<int> {1, 1};

while (fibonacciNumbers.Count < 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);

本文标签: 之路学习心得MSDN