• C#(CSharp)入门教程


    目录

    C#的第一个程序

    变量 

    折叠代码 

    变量类型和声明变量 

    获取变量类型所占内存空间(sizeof) 

    常量 

    转义字符 

    隐式转换 

    显示转换 

    异常捕获 

    运算符 

    算术运算符 

    布尔逻辑运算符

    关系运算符 

    位运算符 

    其他运算符 

    字符串拼接 

    条件分支语句 

    if...else 语句

    switch语句 

    循环语句 

    while语句 

    do while语句 (使用较少)

    for循环 (最常用)

    补充内容 

    随机数 


    C#的第一个程序

     因为笔者学习过Java,所以个人感觉C#上手起来很快,很多都是java中学习过的,所以不再赘述。该系列文章主要是分享给有一定编程经验的小伙伴,但是又想要学习C#的来使用,零基础的可能会比较吃力,所以酌情使用。

    1. using System;
    2. namespace CSharp入门_第一个程序
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. //使用Console调用方法
    9. Console.WriteLine("Hello World");
    10. //WriteLine方法会自动空一行(换行)
    11. //使用Write则不会换行
    12. Console.WriteLine("我爱敲代码");//输出
    13. Console.Write("你好!");
    14. Console.Write("世界");
    15. //Console.ReadKey();//需要使用该语句,不然控制台会一闪而过,或者只执行不调试也可以
    16. Console.WriteLine("请玩家输入名字:");
    17. //C#的输入
    18. Console.ReadLine();
    19. //玩家输入完毕
    20. Console.Write("玩家输入完毕!请开始游戏吧!");
    21. //这里注意!不使用ReadKey是会直接闪过控制台的,所以我们还是可以使用Ctrl+F5
    22. }
    23. }
    24. }

    需要特别注意的是向控制台输入内容:

    1. //向控制台输入信息
    2. Console.ReadLine();
    3. //判断是否按键,只有玩家按键了才会结束(可以用来防止跳过控制台显示)
    4. Console.ReadKey();

    变量 

    折叠代码 

    1. using System;
    2. namespace CSharp入门_变量
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. Console.WriteLine("变量");
    9. //1、折叠代码(防止代码过于凌乱)输入#region按下tab自动补全
    10. #region 声明变量
    11. #endregion
    12. }
    13. }
    14. }

    点击-号后我们可以看到中间部分进行了折叠:

    变量类型和声明变量 

    1. using System;
    2. namespace CSharp入门_变量
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. Console.WriteLine("变量");
    9. //1、折叠代码(防止代码过于凌乱)输入#region按下tab自动补全
    10. #region 声明变量
    11. //有符号整型
    12. //sbyte
    13. sbyte i = 1;
    14. Console.WriteLine("i的值为" + i);
    15. //short
    16. short x = 2;
    17. Console.WriteLine("x的值为" + x);
    18. //int
    19. int y = 3;
    20. Console.WriteLine("y的值为" + y);
    21. //long
    22. long z = 4;
    23. Console.WriteLine("z的值为" + z);
    24. //无符号整型
    25. //byte
    26. byte a = 5;
    27. Console.WriteLine("a的值为" + a);
    28. //ushort
    29. ushort b = 6;
    30. Console.WriteLine("b的值为" + b);
    31. //uint
    32. uint c = 7;
    33. Console.WriteLine("c的值为" + c);
    34. //ulong
    35. ulong d = 8;
    36. Console.WriteLine("d的值为" + d);
    37. //浮点数(小数)
    38. //float
    39. float f = 0.12f;
    40. Console.WriteLine("f的值为" + f);
    41. //double
    42. double db = 0.223;
    43. Console.WriteLine("db的值为" + db);
    44. //deciml
    45. decimal dm = 0.123456789m;
    46. Console.WriteLine("dm的值为" + dm);
    47. //特殊类型
    48. //bool
    49. bool b1 = true;
    50. bool b2 = false;
    51. Console.WriteLine(b1 + "_" + b2);
    52. //char(字符)
    53. char ch = 'A';
    54. Console.WriteLine("ch的值为"+ch);
    55. //string(字符串)
    56. string str = "我要认认真真学CSharp";
    57. Console.WriteLine(str);
    58. #endregion
    59. }
    60. }
    61. }

    获取变量类型所占内存空间(sizeof) 

    1. using System;
    2. namespace CSharp入门_变量
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. Console.WriteLine("变量");
    9. //1、折叠代码(防止代码过于凌乱)输入#region按下tab自动补全
    10. #region 声明变量
    11. //有符号整型
    12. //sbyte
    13. sbyte i = 1;
    14. Console.WriteLine("i的值为" + i);
    15. //short
    16. short x = 2;
    17. Console.WriteLine("x的值为" + x);
    18. //int
    19. int y = 3;
    20. Console.WriteLine("y的值为" + y);
    21. //long
    22. long z = 4;
    23. Console.WriteLine("z的值为" + z);
    24. //无符号整型
    25. //byte
    26. byte a = 5;
    27. Console.WriteLine("a的值为" + a);
    28. //ushort
    29. ushort b = 6;
    30. Console.WriteLine("b的值为" + b);
    31. //uint
    32. uint c = 7;
    33. Console.WriteLine("c的值为" + c);
    34. //ulong
    35. ulong d = 8;
    36. Console.WriteLine("d的值为" + d);
    37. //浮点数(小数)
    38. //float
    39. float f = 0.12f;
    40. Console.WriteLine("f的值为" + f);
    41. //double
    42. double db = 0.223;
    43. Console.WriteLine("db的值为" + db);
    44. //deciml
    45. decimal dm = 0.123456789m;
    46. Console.WriteLine("dm的值为" + dm);
    47. //特殊类型
    48. //bool
    49. bool b1 = true;
    50. bool b2 = false;
    51. Console.WriteLine(b1 + "_" + b2);
    52. //char(字符)
    53. char ch = 'A';
    54. Console.WriteLine("ch的值为"+ch);
    55. //string(字符串)
    56. string str = "我要认认真真学CSharp";
    57. Console.WriteLine(str);
    58. #endregion
    59. int sbyteSize = sizeof(sbyte);
    60. Console.WriteLine("sbyte所占的字节数为:" + sbyteSize);
    61. int shortSize = sizeof(short);
    62. Console.WriteLine("short所占的字节数为:" + shortSize);
    63. int intSize = sizeof(int);
    64. Console.WriteLine("int所占的字节数为:" + intSize);
    65. int longSize = sizeof(long);
    66. Console.WriteLine("long所占的字节数为:" + longSize);
    67. Console.WriteLine("*********************************************");
    68. int byteSize = sizeof(byte);
    69. Console.WriteLine("byte所占的字节数为:" + byteSize);
    70. int uintSize = sizeof(uint);
    71. Console.WriteLine("uint所占的字节数为:" + uintSize);
    72. int ushortSize = sizeof(ushort);
    73. Console.WriteLine("ushort所占的字节数为:" + ushortSize);
    74. int ulongSize = sizeof(ulong);
    75. Console.WriteLine("ulong所占的字节数为:" + ulongSize);
    76. Console.WriteLine("*********************************************");
    77. int floatSize = sizeof(float);
    78. Console.WriteLine("float所占的字节数为:" + floatSize);
    79. int doubleSize = sizeof(double);
    80. Console.WriteLine("double所占的字节数为:" + doubleSize);
    81. int decimalSize = sizeof(decimal);
    82. Console.WriteLine("decimal所占的字节数为:" + decimalSize);
    83. Console.WriteLine("*********************************************");
    84. int boolSize = sizeof(bool);
    85. Console.WriteLine("bool所占的字节数为:" + boolSize);
    86. int charSize = sizeof(char);
    87. Console.WriteLine("char所占的字节数为:" + charSize);
    88. }
    89. }
    90. }

    常量 

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_常量
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("常量");
    13. //常量使用关键字const
    14. const int c = 1;
    15. Console.WriteLine("c的值为"+c);
    16. //常量特点:必须初始化,不可以更改
    17. //使用常量的场景:声明一些常用的不变的量,如π
    18. }
    19. }
    20. }

    转义字符 

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_转义字符
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("转义字符");
    13. /*
    14. * 转义字符的使用
    15. */
    16. //单引号 \'
    17. string str1 = "\'哈哈哈\'";
    18. Console.WriteLine("str1 = "+str1);
    19. //双引号 \"
    20. string str2 = "\"CSharp太好学啦!\"";
    21. Console.WriteLine("str2 = " + str2);
    22. //换行 \n
    23. string str3 = "\"CSharp\n太好学啦!\"";
    24. Console.WriteLine("str3 = " + str3);
    25. //斜杠 \\
    26. string str4 = "\"CSharp\\太好学啦!\"";
    27. Console.WriteLine("str4 = " + str4);
    28. /*
    29. * 下面的是不太常用的转义字符
    30. *
    31. */
    32. //制表符 \t (空一个tab键)
    33. string str5 = "我要好好学\t编程";
    34. Console.WriteLine("str5 = " + str5);
    35. //光标退格 \b (光标退一格进行覆盖)
    36. string str6 = "我要好好学\b编程";
    37. Console.WriteLine("str6 = " + str6);
    38. //警报音 \a 这里会有个警报音响起
    39. string str7 = "\a";
    40. Console.WriteLine(str7);
    41. /*
    42. 取消转义字符 使用@
    43. */
    44. string str8 = @"\n\\";
    45. Console.WriteLine("str8 = "+str8);
    46. }
    47. }
    48. }

    隐式转换 

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_隐式转换
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("隐式转换");
    13. //大范围装小范围,记住这个就可以
    14. long L = 5;
    15. int i = 1;
    16. L = i;
    17. Console.WriteLine("L = "+L);
    18. //一般都是这个规律,但是浮点型中decimal不能隐式转换为float和double
    19. //特殊类型如bool,char,string之间不存在隐式转换,但是char可以隐式转换为整数
    20. //(因为char取值范围是0-65535,其对应的数字 其实是一个ascll码,一个数字就是一个对应关系)
    21. //无符号的不能装有符号的,但是有符号的可以装无符号的,因为无符号数没有负数,而有符号数则有负数
    22. //即有符号数不能隐式转换为无符号数,无符号数可以隐式转换为有符号数(要注意范围是涵盖的,大装小)
    23. uint ui = 4;
    24. L = ui;
    25. Console.WriteLine("L = " +L );
    26. //浮点数可以装任何类型的 整型
    27. float f = L;
    28. Console.WriteLine("f = " + f);
    29. //总结:
    30. //double一>f1oat一>所有整形(无符号、有符号)
    31. //decimal一>所有整形(无符号、有符号)
    32. //整数不能去装浮点数:浮点数不能隐式转换为整数
    33. //总结隐式转换规则
    34. //高精度(大范围)装低精度(小范围)
    35. //double一 > f1oat一 > 整数(无符号、有符号)一 > char
    36. //decimal一 > 整数(无符号、有符号)一 > char
    37. //string和boo1不参与隐式转换规则的
    38. }
    39. }
    40. }

    显示转换 

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_显示转换
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("显示转换");
    13. //1、强制类型转换 将高精度类型强制转换为低精度类型
    14. //语法:变量类型 变量名 = (强转的类型)变量;
    15. //需要注意:精度和范围
    16. short st = 1;
    17. int i = 5;
    18. st = (short)i;
    19. Console.WriteLine("st的值为:" + st);
    20. //如果我们范围超过就会出问题
    21. short so = 2;
    22. int x = 40000;
    23. so = (short)x;
    24. Console.WriteLine("so的值为:" + so);
    25. //注意:在强制类型转换的时候一定要注意范围,在强制转换浮点型的时候会出现精度变低的情况,要注意
    26. //浮点数强转成整形时会直接抛弃掉小数点后面的小数
    27. double b = 1.5999999;
    28. i = (int)b;
    29. Console.WriteLine("i的值为:" + i);
    30. //2、Parse转换 (将字符串类型转换为对应的类型)字符串必须能够转换为对应类型,不然会报错,还有范围
    31. //语法:变量类型.Parse("字符串");
    32. int i2 = int.Parse("123");
    33. Console.WriteLine("i2的值为:" + i2);
    34. //3、Convert转换
    35. //作用:
    36. //更准确的将各个类型之间进行相互转换
    37. //语法:Convert.To目标类型(变量或常量)
    38. //注意:填写的变量或常量必须正确否则出错
    39. int i3 = Convert.ToInt32("1234");
    40. Console.WriteLine("i3的值为:" + i3);
    41. //Convert转换时会四舍五入,精度比强制类型转换高
    42. i3 = Convert.ToInt32(1.688f);
    43. Console.WriteLine("i3的值为:" + i3);
    44. //对于bool也可以转化为int
    45. i3 = Convert.ToInt32(true);
    46. Console.WriteLine("i3的值为:" + i3);
    47. i3 = Convert.ToInt32(false);
    48. Console.WriteLine("i3的值为:" + i3);
    49. float f1 = Convert.ToSingle(12.5);
    50. bool b1 = Convert.ToBoolean("true");
    51. //4、其他类型转string
    52. //变量.ToString();
    53. String str3 = 1.ToString();
    54. Console.WriteLine("str3的值为:" + str3);
    55. String str4 = 1.5f.ToString();
    56. Console.WriteLine("str4的值为:" + str4);
    57. Console.WriteLine("666"+1 +true);//相对于每个都调用了tostring
    58. }
    59. }
    60. }

    异常捕获 

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_异常捕获
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("异常捕获");
    13. #region 作用
    14. //异常捕获作用:避免代码错误造成程序卡死
    15. //string str = Console.ReadLine();
    16. // int i = int.Parse(str);//比如这里的str如果输入的范围大于int就会卡死
    17. #endregion
    18. #region 语法
    19. try
    20. {
    21. //希望进行异常捕获的代码块
    22. //如果try中的代码块出现异常就进入catch
    23. }
    24. catch(Exception e)//可以通过e得到具体的报错信息(可以不写)
    25. {
    26. //如果异常进行的动作
    27. }
    28. finally//可选
    29. {
    30. //不管有没有出错,都会执行其中的代码
    31. }
    32. #endregion
    33. #region 测试
    34. try
    35. {
    36. string str2 = Console.ReadLine();
    37. int i2= int.Parse(str2);
    38. Console.WriteLine(i2);
    39. }
    40. catch
    41. {
    42. Console.WriteLine("请输入合法的数字!");
    43. }
    44. finally
    45. {
    46. Console.WriteLine("执行完毕!");
    47. }
    48. #endregion
    49. }
    50. }
    51. }

    如果输入不合法:

    运算符 

    算术运算符 

    一元运算符描述实例
    ++增量运算符++a 输出 11
    --减量运算符--a 输出 9
    +一元加运算符+a 输出 10
    -一元减运算符-a 输出 -10
    二元运算符描述实例
    *乘法运算符a * b 输出 200
    /除法运算符b / a 输出 2
    %余数运算符b % a 输出 0
    +加法运算符b + a 输出 30
    -减法运算符b - a 输出 10

    布尔逻辑运算符

    布尔运算符描述实例
    !逻辑非运算符!a 为 False
    &逻辑与运算符a & b 为 False
    |逻辑或运算符a | b 为 True
    ^逻辑异或运算符a ^ b 为 True
    &&条件逻辑与运算符a && b 为 False
    ||条件逻辑或运算符a || b 为 True

    关系运算符 

    位运算符 

    位运算符描述实例
    ~按位求补运算符~a 等于 -2
    <<左移位运算符a << 2 等于 4
    >>右移位运算符a >> 2 等于 0
    >>>无符号右移运算符a >>> 2 等于 0
    &逻辑与运算符a & b 等于 0
    ^逻辑异或运算符a ^ b 等于 3
    |逻辑或运算符a | b 等于 3

    其他运算符 

    字符串拼接 

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_字符串拼接
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("字符串拼接");
    13. //1、用+号进行字符串拼接
    14. string str = "123";
    15. Console.WriteLine(str+"456");
    16. str = str + 1;
    17. Console.WriteLine(str);
    18. str = "123";
    19. str += "1" + 4 + true;
    20. Console.WriteLine(str);
    21. str += 1 + 2 + 3 + 4;
    22. Console.WriteLine(str);
    23. str += "" + 1 + 2 + 3;
    24. Console.WriteLine(str);
    25. //2、使用string.Format();
    26. //string.Format("待拼接的内容",内容1,内容2,.....)
    27. //拼接内容中的固定规则
    28. //想要被拼接的内容用占位符替代{数字}数字:n依次往后
    29. string str3;
    30. str3 = string.Format("我爱{0}, 我今年{1}岁, 我想好好学习{2}", "学习",18, "C#");
    31. Console.WriteLine(str3);
    32. str3 = string.Format("{0}{1}{2}", 1, true, false);
    33. Console.WriteLine(str3);
    34. //3、控制台打印
    35. Console.WriteLine("A{0}B{1}C{2}", 1, true, false);
    36. //注意:后面的内容可以比前面多,但是不能比前面少,否则会报错,还有占位符数字要从0开始
    37. }
    38. }
    39. }

    条件分支语句 

    if...else 语句

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_条件分支语句
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("条件分支语句");
    13. //if语句
    14. int i = 0;
    15. int flag = 0;
    16. if (flag == 0)
    17. {
    18. i = 100;
    19. flag = 1;
    20. }
    21. Console.WriteLine(i);
    22. //if...else
    23. if (flag == 1)
    24. {
    25. i = 200;
    26. }
    27. else
    28. {
    29. i = 1;
    30. }
    31. Console.WriteLine(i);
    32. //再举一个
    33. Console.WriteLine("请输入性别:"+"输入男或者女");
    34. string sex = Console.ReadLine();
    35. if (sex == "男")
    36. {
    37. Console.WriteLine("我是男生");
    38. }
    39. else
    40. {
    41. Console.WriteLine("我是女生");
    42. }
    43. }
    44. }
    45. }

    switch语句 

    1. //switch语句
    2. int a = 3;
    3. switch (a)
    4. {
    5. case 1: //条件只能是常量
    6. Console.WriteLine("a = 1");
    7. break;
    8. case 2:
    9. Console.WriteLine("a = 2");
    10. break;
    11. case 3:
    12. Console.WriteLine("a = 3");
    13. break;
    14. default:
    15. Console.WriteLine("什么条件都不满足,执行default语句");
    16. break;
    17. }
    18. //switch也支持自定义常量
    19. int b = 2;
    20. const int x = 2;
    21. switch (b)
    22. {
    23. case 1: //条件只能是常量
    24. Console.WriteLine("b = 1");
    25. break;
    26. case x:
    27. Console.WriteLine("b = 2");
    28. break;
    29. case 3:
    30. Console.WriteLine("b = 3");
    31. break;
    32. default:
    33. Console.WriteLine("什么条件都不满足,执行default语句");
    34. break;
    35. }
    36. //同时需要注意的是switch会进行贯穿
    37. int c = 1;
    38. switch (c)
    39. {
    40. case 1: //条件只能是常量
    41. case 2:
    42. case 3:
    43. Console.WriteLine("c = 3"); //C#的贯穿和java不一样,java的在case后面还可以进行执行语句,而C#后面只能像上述所写
    44. //贯穿到第一个break,所以结果是c = 3
    45. break;
    46. default:
    47. Console.WriteLine("什么条件都不满足,执行default语句");
    48. break;
    49. }

    循环语句 

    while语句 

    写循环语句的时候一般要避免写死循环(条件一直为真) 还会造成程序卡死

    但是有时候也会用到死循环

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_循环语句
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("循环语句");
    13. //while语句
    14. //while (true)
    15. //{
    16. // Console.WriteLine("这是一个死循环");
    17. //}
    18. //巧用死循环
    19. string password = "123456";
    20. while (true)
    21. {
    22. Console.WriteLine("请用户输入密码:");
    23. string str = Console.ReadLine();
    24. if (str == password)
    25. {
    26. Console.WriteLine("登录成功!");
    27. break;
    28. }
    29. }
    30. }
    31. }
    32. }

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace CSharp入门_循环语句
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.WriteLine("循环语句");
    13. //while语句
    14. //while (true)
    15. //{
    16. // Console.WriteLine("这是一个死循环");
    17. //}
    18. //巧用死循环
    19. string password = "123456";
    20. while (true)
    21. {
    22. Console.WriteLine("请用户输入密码:");
    23. string str = Console.ReadLine();
    24. if (str == password)
    25. {
    26. Console.WriteLine("登录成功!");
    27. break;//跳出循环
    28. }
    29. }
    30. int i = 0;
    31. while (i < 10)
    32. {
    33. i++;
    34. }
    35. Console.WriteLine("i = "+i);
    36. }
    37. }
    38. }

    1. //打印1到20之间的奇数
    2. int index = 0;
    3. while (index < 20)
    4. {
    5. ++index;
    6. //什么样的数是奇数
    7. //不能被2整除的数一>%
    8. if (index % 2 == 0)
    9. continue; //回到循环开始,继续执行
    10. Console.WriteLine(index);
    11. }

    do while语句 (使用较少)

    do while语句和while的不同在于do while是先执行一次再进行判断,其他基本一致。

    1. int a = 0;
    2. do
    3. {
    4. a++;
    5. } while (a<5);
    6. Console.WriteLine(a);//如果使用while的话这里的a就变成4了

    需要注意一点的是:continue在该语句中使用的时候是跳转到while进行判断,而不是从上到下 

    for循环 (最常用)

    for循环特别适合在某个范围内进行取值的操作,一般while能做的,for循环都可以做

    1. for (int p = 0; p < 10; p++)
    2. {
    3. Console.WriteLine(p);
    4. }
    5. //for (; ; )
    6. //{
    7. // Console.WriteLine("for循环的死循环");
    8. //}
    9. int i ;
    10. for (i = 0; i < 10; i++)
    11. {
    12. for(int j = 0; j < 10; j++)
    13. {
    14. Console.WriteLine(i+"_"+j);
    15. }
    16. }

    补充内容 

    1. //输入一个键并赋值
    2. char c = Console.ReadKey().KeyChar;
    3. //输入一个键并赋值 且不在控制台显示
    4. char c = Console.ReadKey(true).KeyChar;
    5. //清除控制台
    6. Console.Clear();
    7. //设置光标位置 控制台左上角为0,0 右侧是x正方向 下方是y正方向
    8. Console.SetCursorPosition(10,5);
    9. //设置文字颜色
    10. Console.ForegroundColor = ConsoleColor.Red;
    11. //设置背景色 配合Clear使用 填充窗口颜色
    12. Console.BackgroundColor = ConsoleColor.White;
    13. Console.Clear();
    14. //光标显隐
    15. Console.CursorVisible = false;
    16. //关闭控制台
    17. Environment.Exit(0);

    随机数 

     

    好了,C#(CSharp)入门教程到这里就基本结束了,全程都由本人在线学习进行敲的代码,可以直接拿来实验,多敲代码,才能掌握的更好哈!加油,接着学!

  • 相关阅读:
    RK3566恢复显示屏异常显示的方法
    ElasticSearch安装、配置详细步骤
    C++模拟OpenGL库——图形学状态机接口封装(一):用状态模式重构部分代码及接口定义
    GIS工具maptalks开发手册(二)02——渲染线
    java中okhttp和httpclient那个效率高
    [QCM6125][Android10] 系统设置隐藏搜索栏以及右上角的搜索按钮
    860. 柠檬水找零
    文本框内容自动投影,浅谈C#中事件的写法与应用
    HBase shell常用命令
    前端食堂技术周刊第 52 期:Babel 7.19.0、Fresh 1.1、React Native 0.70、新的 Web 性能指标 INP
  • 原文地址:https://blog.csdn.net/m0_67995737/article/details/133317121