C#入门笔记(需有C基础)

一、

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#开发『微软官方课』 笔记,感兴趣的可以去了解更多。
如有错误,欢迎指出。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值