赞
踩
由于c#官方学习文档提供的是交互式学习代码模块及浏览器模拟练习器,其中与实际的代码和编程环境还是有点差别的!为了帮读者们能有效并快速的学习c#,特意写了这个博客。
这个博客的主要内容是:集成和汇总主要的官方文档的学习代码并且能在实际的编译环境中使用运行,但为了快速学习,所以在代码模块没有使用正常的格式(没有写出完整的代码块),选择更加简单的引用格式和代码,因为这能帮助我们在练习熟悉时c#的使用语法等等,但是在正式学习后,请务必使用正常的格式和代码块!
本编程环境在VS Code运行编写的代码,因为既方便也使用起来简单!
由于我们直接使用的是VS code,所以我们要手动建立一个c#相关工程;下面给出链接,读者可自己查看教程创建,因为比较简单!
配置使用VisualStudioCode编写运行C#
注意:在项目文件目录下可直接创建练习的.cs文件,方便编写和修改!
using System;
Console.WriteLine("Hello World!");//打印出来
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!");
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()}]");//同时去掉字符串前后的空格的方法
using static System.Console;
string a = "Hello,steve!";
WriteLine(a);
WriteLine(a.Replace("Hello","Hi"));//替换方法,第一个参数是目标,第二个参数是代替的内容
WriteLine(a.ToLower());//替换成小写
WriteLine(a.ToUpper());//替换成大写
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"));//末端匹配字符串
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);
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);
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}");//取余
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}");
using static System.Console;
double a = 19;
double b = 23;
double c = 8;
double d = (a + b) / c;
WriteLine(d);
using static System.Console;
double max = double.MaxValue;
double min = double.MinValue;//双精度值的范围远大于整数值
WriteLine($"The range of double is {min} to {max}");//打印出来的这些值用科学记数法表示。 E 左侧为有效数字。 右侧为是 10 的 n 次幂。
using static System.Console;
double third = 1.0 / 3.0;
WriteLine(third);
using static System.Console;
decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
WriteLine($"The range of the decimal type is {min} to {max}");
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 类型,十进制类型的精度更高
using static System.Console;
using static System.Math;//PI的声明引用
double radius = 2.50;
double area = PI * radius * radius;
WriteLine(area);
using static System.Console;
int a = 8;
int b = 9 ;
if (a + b > 10)
WriteLine(a+b);
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");
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");
}
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");
}
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");
}
using static System.Console;
int count =0;
while (count <10){
WriteLine($"Hello,steve!The count is {count} .");
count++;
}
using static System.Console;
int count =0;
do {
WriteLine($"Hello,steve!The count is {count} .");
count++;
}while (count < 10);//while 循环先测试条件,然后再执行 while 后面的代码。 do ... while 循环先执行代码,然后再检查条件
using static System.Console;
for(int count =0;count<10;count++)
{
WriteLine($"Hello,steve!The count is {count} .");
}
using static System.Console;
for (int row = 1; row < 11; row++)
{
for (char column = 'a'; column < 'k'; column++)
{
WriteLine($"The cell is ({row}, {column}) .");
}
}
using static System.Console;
int sum =0;
for(int i =1;i<21;i++)
{
if (i%3==0)
{
sum=sum +i;
}
}
WriteLine(sum);
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");
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()}!");
}
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);
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);
最后,文中如有不足,欢迎批评指正!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。