目录
一、
1.Console.WriteLine()
//1.输出:Hello Friends
Console.WriteLine("Hello Friends");
//2.使用string
//2.1(输出:Bob)
string af="Bob";
Console.WriteLine(af);
//2.2(输出:Hello Bob)
string af="Bob";
Console.WriteLine("Hello "+af);
//2.3(输出:Hello Bob)
string af="Bob";
Console.WriteLine($"Hello {af}");
//2.4(输出:My friend are Bob and Enna)
string ff="Bob";
string sf="Enna";
Console.WriteLine($"My friend are {ff} and {sf}");
2.Length()
代码:
string ff="Bob";
Console.WriteLine($"The name {ff} has {ff.Length} letters.");
输出:
The name Bob has 3 letters.
3.Trim()、TrimStart()、TrimEnd()
代码:
//去掉空格
string g=" Hello World ";
Console.WriteLine($"[{g}]");
string tg=g.TrimStart();
Console.WriteLine($"[{tg}]");
tg=g.TrimEnd();
Console.WriteLine($"[{tg}]");
tg=g.Trim();
Console.WriteLine($"[{tg}]");
输出:
[ Hello World ]
[Hello World ]
[ Hello World]
[Hello World]
4.Replace()
代码:
string sayHello="Hello World!";
Console.WriteLine(sayHello);
sayHello=sayHello.Replace("Hello","Greetings");
Console.WriteLine(sayHello);
输出:
Hello World!
Greetings World!
5.ToUpper()、ToLower()
代码:
string sayHello="Hello World!";
Console.WriteLine(sayHello);
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());
输出:
Hello World!
HELLO WORLD!
hello world!
6.Contains()
代码:
string sayHello="Hello World!";
Console.WriteLine(sayHello.Contains("Hello"));
Console.WriteLine(sayHello.Contains("He"));
Console.WriteLine(sayHello.Contains("HE"));
输出:
True
True
False
7.StartsWith()、EndsWith()
代码:
string sayHello="Hello World!";
Console.WriteLine(sayHello.StartsWith("Hello"));
Console.WriteLine(sayHello.StartsWith("d"));
var result=sayHello.EndsWith("!");
Console.WriteLine(result);
输出:
True
False
True
8.decimal
代码:
//比double范围小,但精度高。
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
二、
1.数组的使用
代码:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var names=new List<string>{"<name>","Scott","Kendra"};
//var 变量(当你目前还不明确用什么类型时使用)
//List<T> 泛型
names.Add("Maria");
names.Add("Bill");
names.Remove("Scott");
foreach(var name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("names[0]:"+names[0]);
}
}
输出:
<name>
Kendra
Maria
Bill
names[0]:<name>
2.IndexOf()
代码:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var names=new List<string>{"WEIRD","Scott","Kendra"};
names.Add("Maria");
names.Add("Bill");
names.Remove("Scott");
foreach(var name in names)
{
Console.WriteLine(name);
}
var index=names.IndexOf("Bill");
Console.WriteLine($"Found Bill at {index}");
var index2=names.IndexOf("Scott");
Console.WriteLine($"Found Bill at {index2}");
}
}
输出:
WEIRD
Kendra
Maria
Bill
Found Bill at 3
Found Bill at -1
3.Sort()
代码:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var names=new List<string>{"Scott","Kendra","Enna"};
foreach(var name in names)
{
Console.WriteLine(name);
}
Console.WriteLine("sort后的数组:");
names.Sort();
foreach(var name in names)
{
Console.WriteLine(name);
}
}
}
输出:
Scott
Kendra
Enna
sort后的数组:
Enna
Kendra
Scott
4.Count
代码:
var names=new List<string>{"Scott","Kendra","Enna"};
Console.WriteLine($"数组的长度为:{names.Count}");
输出:
数组的长度为:3
5.斐波那契数列
代码:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var f=new List<int>{1,1};
while(f.Count<20)
{
var p=f[f.Count-1];
var p2=f[f.Count-2];
f.Add(p+p2);
}
foreach(var item in f)
{
Console.WriteLine(item);
}
}
}
输出:
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
注:此为b站零基础入门学习C#开发『微软官方课』 笔记,感兴趣的可以去了解更多。
如有错误,欢迎指出。