C#官方学习入门文档练习代码汇总和分享

前言

由于c#官方学习文档提供的是交互式学习代码模块及浏览器模拟练习器,其中与实际的代码和编程环境还是有点差别的!为了帮读者们能有效并快速的学习c#,特意写了这个博客。
这个博客的主要内容是:集成和汇总主要的官方文档的学习代码并且能在实际的编译环境中使用运行,但为了快速学习,所以在代码模块没有使用正常的格式(没有写出完整的代码块),选择更加简单的引用格式和代码,因为这能帮助我们在练习熟悉时c#的使用语法等等,但是在正式学习后,请务必使用正常的格式和代码块!
本编程环境在VS Code运行编写的代码,因为既方便也使用起来简单!

在VS code中编写C#脚本

前期准备

由于我们直接使用的是VS code,所以我们要手动建立一个c#相关工程;下面给出链接,读者可自己查看教程创建,因为比较简单!
配置使用VisualStudioCode编写运行C#

注意:在项目文件目录下可直接创建练习的.cs文件,方便编写和修改!
在这里插入图片描述

1.11运行首个 C# 程序

using System;
Console.WriteLine("Hello World!");//打印出来

1.12声明和使用变量

using static System.Console;
string a="steve draw";//字符串内容由一对双引号构成,并且初始化时要先声明变量类型
WriteLine(a);//打印字符串变量a
WriteLine("hello,"+a);//打印字符串与字符串变量
WriteLine($"hello,{a}!");//打印字符串中嵌套的变量
WriteLine($"The name {a} has {a.Length} letters!");

1.13字符串的含有空格的处理方法

using static System.Console;
string a="      Hello World!       ";
string b="      Hello World!       ";
string c="      Hello World!       ";
WriteLine($"[{a.TrimStart()}]");//去掉字符串前面的空格
WriteLine($"[{b.TrimEnd()}]");//去掉字符串后面的空格
WriteLine($"[{c.Trim()}]");//同时去掉字符串前后的空格的方法

1.14字符串处理方法之替换和大小写转换

using static System.Console;
string a = "Hello,steve!";
WriteLine(a);
WriteLine(a.Replace("Hello","Hi"));//替换方法,第一个参数是目标,第二个参数是代替的内容
WriteLine(a.ToLower());//替换成小写
WriteLine(a.ToUpper());//替换成大写

1.15字符串处理方法之匹配字符串

using static System.Console;
string a = "Hello,steve!";
WriteLine(a.Contains("bye"));//
WriteLine(a.Contains("steve"));//全局查找匹配字符串,返回布尔值
WriteLine(a.StartsWith("Hello"));//前端匹配字符串
WriteLine(a.EndsWith("see you"));//末端匹配字符串

1.16int类型数值基础运算

using static System.Console;
int a = 8;
int b = 9;
int c = a + b;//加运算
int d =a-b;//减运算
int e = a * b;//乘运算
int f = b /a;//除运算
WriteLine(c);
WriteLine(d);
WriteLine(e);
WriteLine(f);

1.17int类型数值复杂运算

using static System.Console;
int a = 5;
int b = 4;
int c = 2;
int d = a + b * c;
int e = (a + b) * c;
int f = (a + b) - 6 * c + (12 * 4) / 3 + 12;
int g = (a + b) / c;
WriteLine(d);
WriteLine(e);
WriteLine(f);
WriteLine(g);

1.18int类型之整除和取余

using static System.Console;
int a = 7;
int b = 4;
int c = 3;
int d = (a + b) / c;
int e = (a + b) % c;
WriteLine($"quotient: {d}");//整除结果
WriteLine($"remainder: {e}");//取余

1.19int类型范围大小以及溢出问题

using static System.Console;
int max = int.MaxValue;//最大上限值
int min = int.MinValue;//最小上限值
WriteLine($"The range of integers is {min} to {max}");
int what = max + 3;//因为溢出从最大整数值覆盖回最小整数值
WriteLine($"An example of overflow: {what}");

1.2double类型基本运算

using static System.Console;
double a = 19;
double b = 23;
double c = 8;
double d = (a + b) / c;
WriteLine(d);

1.21double类型范围大小比较

using static System.Console;
double max = double.MaxValue;
double min = double.MinValue;//双精度值的范围远大于整数值
WriteLine($"The range of double is {min} to {max}");//打印出来的这些值用科学记数法表示。 E 左侧为有效数字。 右侧为是 10 的 n 次幂。

1.22double类型的精度

using static System.Console;
double third = 1.0 / 3.0;
WriteLine(third);

1.23decimal类型范围大小

using static System.Console;
decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
WriteLine($"The range of the decimal type is {min} to {max}");

1.24decimal类型与double类型精度比较

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

decimal c = 1.0M;
decimal d = 3.0M;
WriteLine(c / d);//范围小于 double 类型,十进制类型的精度更高

1.25double类型运算体现(圆面积运算例子)

using static System.Console;
using static System.Math;//PI的声明引用
double radius = 2.50;
double area = PI * radius * radius;
WriteLine(area);

2.11条件结构之if结构

using static System.Console;
int a = 8;
int b = 9 ;
if (a + b > 10)
    WriteLine(a+b);

2.12条件结构之if-else结构

using static System.Console;
int a = 5;
int b = 9;
if (a + b > 10)
    WriteLine("The answer is greater than 10");
else
    WriteLine("The answer is not greater than 10");

2.13条件结构之if-else结构使用形式

using static System.Console;
int a = 5;
int b = 9;
if (a + b > 10)
{
    WriteLine("The answer is greater than 10");
}
    
else
    {
        WriteLine("The answer is not greater than 10");
    }

2.14条件结构之if-else结构(且条件)

using static System.Console;
int a = 5;
int b = 3;
int c = 4;
if ((a + b + c > 10) && (a == b))//&& 表示“且”
{
    WriteLine("The answer is greater than 10");
    WriteLine("And the first number is equal to the second");
}
else
{
    WriteLine("The answer is not greater than 10");
    WriteLine("Or the first number is not equal to the second");
}

2.15条件结构之if-else结构(或条件)

using static System.Console;
int a = 5;
int b = 3;
int c = 4;
if ((a + b + c > 10) || (a == b))// || 表示“或”
{
    WriteLine("The answer is greater than 10");
    WriteLine("Or the first number is equal to the second");
}
else
{
    WriteLine("The answer is not greater than 10");
    WriteLine("And the first number is not equal to the second");
}

2.16while循环

using static System.Console;
int count =0;
while (count <10){
    WriteLine($"Hello,steve!The count is {count} .");
    count++;
}

2.17do-while循环

using static System.Console;
int count =0;
do {
    WriteLine($"Hello,steve!The count is {count} .");
    count++;
}while (count < 10);//while 循环先测试条件,然后再执行 while 后面的代码。 do ... while 循环先执行代码,然后再检查条件

2.18for循环

using static System.Console;
for(int count =0;count<10;count++)
{
    WriteLine($"Hello,steve!The count is {count} .");
}

2.19for循环的嵌套使用

using static System.Console;
for (int row = 1; row < 11; row++)
{
  for (char column = 'a'; column < 'k'; column++)
  {
    WriteLine($"The cell is ({row}, {column}) .");
  }
}

2.2for循环与条件结构的复合使用例子

using static System.Console;
int sum =0;
for(int i =1;i<21;i++)
{
    if (i%3==0)
    {
        sum=sum +i;
    }
}
WriteLine(sum);

3.11数组列表的使用

using static System.Console;
using System.Collections.Generic;
var names = new List<string> { "<name>", "Ana", "Felipe" };
foreach (var name in names)
{
  WriteLine($"Hello {name.ToUpper()}!");
}
WriteLine();
names.Add("Maria");
names.Add("Bill");
names.Remove("Ana");
foreach (var name in names)
{
  WriteLine($"Hello {name.ToUpper()}!");
}
WriteLine();
WriteLine($"My name is {names[0]}.");
WriteLine($"I've added {names[2]} and {names[3]} to the list.");
WriteLine($"The list has {names.Count} people in it");

3.12数组列表的使用例一

using static System.Console;
using System.Collections.Generic;
var names = new List<string> { "name", "Ana", "Felipe" };
var index = names.IndexOf("Felipe");
if (index != -1)
  WriteLine($"The name {names[index]} is at index {index}");

var notFound = names.IndexOf("Not Found");
WriteLine($"When an item is not found, IndexOf returns {notFound}");
names.Sort();
foreach (var name in names)
{
  WriteLine($"Hello {name.ToUpper()}!");
}

3.13数组列表的使用例二

using static System.Console;
using System.Collections.Generic;
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)
    WriteLine(item);

3.14数组列表的使用例三

using static System.Console;
using System.Collections.Generic;
var lists = new List<int> {1,1};
while (lists.Count < 20)
{
    var count_start =lists[lists.Count-2];
    var count_end=lists[lists.Count-1];
    lists.Add(count_start+count_end);
}
foreach(var item in lists)
    WriteLine(item);

最后,文中如有不足,欢迎批评指正!

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值