跳过正文
  1. 文章/
  2. C#/

1、C#

·1685 字·4 分钟· loading · loading · ·
C#
GradyYoung
作者
GradyYoung
C# - 点击查看当前系列文章
§ 1、C# 「 当前文章 」

C# 是一个现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的,由 Ecma 和 ISO 核准认可的。

C# 是由 Anders Hejlsberg 和他的团队在 .Net 框架开发期间开发的。

C# 是专为公共语言基础结构(CLI)设计的。CLI 由可执行代码和运行时环境组成,允许在不同的计算机平台和体系结构上使用各种高级语言。

环境准备
#

Windows开发
#

推荐使用Visual Studio,微软的旗舰IDE(集成开发环境),提供全面的开发工具集,支持C#以及其他多种编程语言。

下载地址:https://visualstudio.microsoft.com/zh-hans/downloads/

对于C#开发, 确保选中.NET desktop development组件(.NET 桌面开发组件),这将包含C#语言支持和Windows桌面开发所需的工具。

Mac或Linux开发
#

下载并安装.NET环境:https://dotnet.microsoft.com/en-us/download

如果需要卸载.NET环境,使用如下脚本即可

#!/usr/bin/env bash
#
# Copyright (c) .NET Foundation and contributors. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

current_userid=$(id -u)
if [ $current_userid -ne 0 ]; then
    echo "$(basename "$0") uninstallation script requires superuser privileges to run" >&2
    exit 1
fi

# this is the common suffix for all the dotnet pkgs
dotnet_pkg_name_suffix="com.microsoft.dotnet"
dotnet_install_root="/usr/local/share/dotnet"
dotnet_path_file="/etc/paths.d/dotnet"
dotnet_tool_path_file="/etc/paths.d/dotnet-cli-tools"

remove_dotnet_pkgs(){
    installed_pkgs=($(pkgutil --pkgs | grep $dotnet_pkg_name_suffix))
    
    for i in "${installed_pkgs[@]}"
    do
        echo "Removing dotnet component - \"$i\"" >&2
        pkgutil --force --forget "$i"
    done
}

remove_dotnet_pkgs
[ "$?" -ne 0 ] && echo "Failed to remove dotnet packages." >&2 && exit 1

echo "Deleting install root - $dotnet_install_root" >&2
rm -rf "$dotnet_install_root"
rm -f "$dotnet_path_file"
rm -f "$dotnet_tool_path_file"

echo "dotnet packages removal succeeded." >&2
exit 0

HelloWorld
#

创建一个目录,然后在该目录内执行命令:dotnet new console,在当前目录中创建一个控制台项目

然后在Program.cs中,写入如下代码

using System;
namespace HelloWorldApplication
{
    class Program
    {
        static void Main(string[] args)
        {
        /*
            这是一个
            多行注释
        */
        // 这一行是注释
        Console.WriteLine("Hello World");
        Console.ReadKey();
        }
    }
}

然后执行命令dotnet run即可看到输出。

  • 程序的第一行 using System; - using 关键字用于在程序中包含 System 命名空间。 一个程序一般包含有多个 using 语句。
  • 下一行是 namespace 声明。一个 namespace 里包含了一系列的类。HelloWorldApplication 命名空间包含了类 Program
  • 下一行是 class 声明。类 Program 包含了程序使用的数据和方法声明。类一般包含多个方法。方法定义了类的行为。在这里,Program 类只有一个 Main 方法。
  • 下一行定义了 Main 方法,是所有 C# 程序的 入口点Main 方法说明当执行时类将做什么动作。
  • 下一行 /*...*/ 将会被编译器忽略,且它会在程序中添加额外的 注释
  • Main 方法通过语句Console.WriteLine("Hello World"); 指定了它的行为。
    • WriteLine 是一个定义在 System 命名空间中的 Console 类的一个方法。该语句会在屏幕上显示消息 Hello World
  • 最后一行 Console.ReadKey(); 是针对 VS.NET 用户的。这使得程序会等待一个按键的动作,防止程序从 Visual Studio .NET 启动时屏幕会快速运行并关闭。

与 Java 不同的是,文件名可以不同于类的名称。

顶级语句
#

在 C# 9.0 版本中,引入了顶级语句(Top-Level Statements)的概念,这是一种新的编程范式,允许开发者在文件的顶层直接编写语句,而不需要将它们封装在方法或类中。

特点:

  • 无需类或方法:顶级语句允许你直接在文件的顶层编写代码,无需定义类或方法。
  • 文件作为入口点:包含顶级语句的文件被视为程序的入口点,类似于 C# 之前的 Main 方法。
  • 自动 Main 方法:编译器会自动生成一个 Main 方法,并将顶级语句作为 Main 方法的主体。
  • 支持局部函数:尽管不需要定义类,但顶级语句的文件中仍然可以定义局部函数。
  • 更好的可读性:对于简单的脚本或工具,顶级语句提供了更好的可读性和简洁性。
  • 适用于小型项目:顶级语句非常适合小型项目或脚本,可以快速编写和运行代码。
  • 与现有代码兼容:顶级语句可以与现有的 C# 代码库一起使用,不会影响现有代码。

命名空间
#

命名空间的设计目的是提供一种让一组名称与其他名称分隔开的方式。在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突

定义
#

namespace namespace_name
{
   // 代码声明
}

using 关键字
#

using 关键字表明程序使用的是给定命名空间中的名称。例如,我们在程序中使用 System 命名空间,其中定义了类 Console。我们可以只写:

Console.WriteLine ("Hello there");

我们可以写完全限定名称,如下:

System.Console.WriteLine("Hello there");

一个文件中可以使用多个命名空间

using System;
using first_space;
using second_space;

namespace first_space
{
   class abc
   {
      public void func()
      {
         Console.WriteLine("Inside first_space");
      }
   }
}
namespace second_space
{
   class efg
   {
      public void func()
      {
         Console.WriteLine("Inside second_space");
      }
   }
}   
class TestClass
{
   static void Main(string[] args)
   {
      abc fc = new abc();
      efg sc = new efg();
      fc.func();
      sc.func();
      Console.ReadKey();
   }
}
C# - 点击查看当前系列文章
§ 1、C# 「 当前文章 」