仓库源文站点原文

知乎文章:中文命名代码示例之 C# 入门

第一行代码

Console.WriteLine("吃了么!");

变量

string 名 = "大黄";
int 邮件数 = 3;
float 温度 = 34.4F;

Console.WriteLine("嘿," + 名 + "! 收件箱有 " + 邮件数 + " 封邮件。气温 " + 温度 + " 度。");
Console.WriteLine(34.40M);

二维数组(节选):

void 显示各组(string[,] 各组) {
    for (int i = 0; i < 各组.GetLength(0); i++) {
        Console.Write($"组 {i + 1}: ");
        for (int j = 0; j < 各组.GetLength(1); j++) Console.Write($"{各组[i,j]}  ");
        Console.WriteLine();
    }
}

添加逻辑

Random 币 = new Random();
Console.WriteLine(币.Next(0, 2) == 0? "花" : "字");
string 所有权限 = "管理员|经理";
int 级别 = 55;

if (所有权限.Contains("管理员")) {
  Console.WriteLine(
    级别 > 55
    ? "欢迎您,超级管理员用户。"
    : "欢迎您, 管理员用户。");
} else if (所有权限.Contains("经理")) {
  Console.WriteLine(
    级别 >= 20
    ? "联系管理员请求访问。"
    : "权限不足");
} else {
  Console.WriteLine("权限不足");
}

【随想】用 if ... else if ... else;相对 if ... elif ... else,只需用 if else 两个词,减少一个 elif

for循环

for (int 数 = 1; 数<=100; 数++) {
    string 行 = "" + 数;
    if (数%3 == 0) {
      if (数%5 == 0) 行 += " - FizzBuzz";
      else 行 += " - Fizz";
    } else if (数%5 == 0) 行 += " - Buzz";
    Console.WriteLine(行);
}

方法

【随想】方法的命名风格开头大写,有点不习惯。但 之后练习 又有小写开头 tellFortune,不明所以。

Console.WriteLine("生成随机数");
显示随机数();

void 显示随机数() {
    Random 生成器 = new Random();

    for (int i = 0; i < 5; i++) {
        Console.Write($"{生成器.Next(1, 100)} ");
    }

    Console.WriteLine();
}

创建可重用方法

Random random = new Random();
int 运 = random.Next(100);

string[] 开头 = {"You have much to", "Today is a day to", "Whatever work you do", "This is an ideal time to"};
string[] 上 = {"look forward to.", "try new things!", "is likely to succeed.", "accomplish your dreams!"};
string[] 下 = {"fear.", "avoid major decisions.", "may have unexpected outcomes.", "re-evaluate your life."};
string[] 中 = {"appreciate.", "enjoy time with friends.", "should align with your values.", "get in tune with nature."};

Console.WriteLine("算命的轻声而言:");
算命();

void 算命() {
  var 命 = (运 > 75 ? 上 : (运 < 25 ? 下 : 中));
  for (int i = 0; i < 4; i++) 
  {
      Console.Write($"{开头[i]} {命[i]} ");
  }
}

方法带返回值

Random 随机生成 = new Random();

Console.WriteLine("试试?(Y/N)");
if (玩否()) 开玩();

bool 玩否() {
    string 意向 = Console.ReadLine();
    return 意向.ToLower().Equals("y");
}

void 开玩() {
    var 继续 = true;

    while (继续) {
        var 目标 = 生成目标();
        var 手气 = 丢色子();

        Console.WriteLine($"大于 {目标} 即胜!");
        Console.WriteLine($"你丢了 {手气} 点");
        Console.WriteLine(判胜负(手气, 目标));
        Console.WriteLine("\n继续?(Y/N)");

        继续 = 玩否();
    }
}

int 生成目标() {
  return 随机生成.Next(1, 6);
}

int 丢色子() {
  return 随机生成.Next(1, 7);
}

string 判胜负(int 手气, int 目标) {
  return 手气 > 目标 ? "You win!" : "You lose!";
}

报错信息

不初始化变量的情况下使用 var 关键字

var message;
error CS0818: 隐式类型化的变量必须已初始化
error CS0818: Implicitly-typed variables must be initialized

类型不可隐式转换

var 问好 = "吃了吗";
问好 = 10.703m;

中英文报错:

error CS0029: 无法将类型“decimal”隐式转换为“string”
error CS0029: Cannot implicitly convert type 'decimal' to 'string'

类似:

error CS0029: 无法将类型“int”隐式转换为“string”

方法相关

error CS0103: 当前上下文中不存在名称“ShouldPlay”

其他

float 温度 = 34.4;

【随想】这里 Double 和 double 的语义区别值得推敲

error CS0664: 无法将 Double 类型隐式转换为“float”类型;请使用“F”后缀创建此类型
error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
error CS1002: 应输入 ;

【待续】

附录

安装 vsc简体中文包 并重启切换vsc语言后,报错等信息即变为中文如:error CS1003: 语法错误,应输入“(”

常用命令


https://learn.microsoft.com/en-us/training/modules/challenge-project-develop-branching-looping-structures-c-sharp/1-introduction

ms-dotnettools.csharp requested to download the .NET Runtime. ms-dotnettools.csharp requested to download the .NET Runtime. ms-dotnettools.csharp tried to install .NET 7.0.15~arm64 but that install had already been requested. No downloads or changes were made. Downloading .NET version(s) 7.0.15~arm64 ........ Done! .NET 7.0.15~arm64 executable path: /Users/xuanwu/Library/Application Support/Code/User/globalStorage/ms-dotnettools.vscode-dotnet-runtime/.dotnet/7.0.15~arm64/dotnet

安装.net core arm64