Samxander's home

You shall see the difference now that we are back again!

0%

C#初步学习

C#初步学习

Foreword

During last term’s study, I found that all of my studies were focused on theories and abstract concepts. What I lacked was practical experience, such as working on a concrete project. Thus, I wanna make a new game. I’m an avid rhythm game player. tbh, It’s really cool to make a rhythm game by myself. After looking up references and websites, I decided to create a traditional 4K rhythm game based on Unity , a game engine including abundant functions and frameworks. Therefore, it’s essential for me to study C# as an important programming language.

简介

  • C# 是一种由 CC++ 衍生出来的面向对象的编程语言,它不仅去掉了 C++ 中的一些复杂特性,还提供了可视化工具,能够高效地编写程序。
  • C# 是由 CC++ 衍生出来的一种安全的、稳定的、简单的、优雅的面向对象编程语言。它在继承C和C++强大功能的同时去掉了一些它们的复杂特性 (例如没有宏以及不允许多重继承) 。
  • C# 使得 C++ 程序员可以高效的开发程序,且因可调用由 C/C++ 编写的本机原生函数,而绝不损失C/C++ 原有的强大的功能。因为这种继承关系,C#C/C++ 具有极大的相似性,熟悉类似语言的开发者可以很快的转向 C#

一个简单的 Hello World!

一个 C# 程序主要包含以下部分:

  • 命名空间的声明
  • 一个 class
  • class 方法
  • class 属性
  • 一个 Main 方法
  • 语句&表达式
  • 注释

C# 文件的后缀为 .cs

我们在 Visual Studio 中创建一个 test.cs 文件,包含了可以打印出 Hello World 的简单代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* 我的第一个C#程序 */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}

数据类型

C# 中,变量分为以下几种类型:

  • 值类型
  • 引用类型
  • 指针类型

值类型

值类型变量可以直接分配给一个值,它们是从类 System.ValueType 中派生的。

如下:

  • bool
  • byte: 8位无符号整数类型
  • char
  • decimal
  • double
  • float
  • int
  • long
  • sbyte:8位有符号整数类型
  • short
  • uint
  • ulong
  • ushort

引用类型

  • 引用类型不包含存储在变量中的实际数据,但它们包含对变量的引用

  • 换句话说,它们指的是一个内存位置。使用多个变量时,引用类型可以指向一个内存位置。如果内存位置的数据是由一个变量改变的,其他变量会自动反映这种值的变化。内置的引用类型有:object、dynamic 和 string。

对象类型

对象类型是 C# 通用类型系统中所有数据类型的终极基类。ObjectSystem.Object 类的别名。所以对象类型可以被分配任何其他类型的值。但是在分配值之前,需要先进行类型转换。当一个值类型转换为对象类型时,则被称为装箱;另一方面,当一个对象类型转换为值类型时,则被称为拆箱

1
2
object obj;
obj = 100; //这是装箱

动态类型

可以存储任何类型的值在动态数据类型变量中。这些变量的类型检查是在运行时发生的。

声明动态类型的语法:

1
2
3
dynamic <variable_name> = value;
// 例如:
dynamic d = 20;

字符串类型

字符串类型允许给变量分配人格字符串值。字符串类型是 System.String 类的别名。他是从对象类型派生的。字符串类型的值可以通过两种形式进行分配:引号和 @ 引号

例如:

1
String str = "WoShiNaiLong";

C# string字符串的前面可以加 @ 将转义字符当做普通字符对待,比如:

1
2
3
string str = @"C:\Windows";
//等价于:
string str = "C:\\Windows";

@ 字符串中可以任意换行,换行符及缩进空格都计算在字符串长度之内。

1
2
3
4
string str = @"<script type=""text/javascript"">
<!--
-->
</script>";

用户自定义引用类型有:classinterfacedelegate。我们将在以后讨论这些类型。

指针类型

指针类型变量存储另一个类型的内存地址,C# 中的指针与 CC++ 中的指针有相同的功能,声明指针类型的语法:

1
type* identifier;

例如:

1
2
char* cptr;
int* iptr;

C#类型转换

类型转换从根本上说是类型铸造,或者说是把数据从一种类型转换为另一种类型。在 C# 中,类型铸造有两种形式:

  • 隐式类型转换:这些转换是 C# 默认的以安全方式进行的转换, 不会导致数据丢失。例如,从小的整数类型转换为大的整数类型,从派生类转换为基类。
  • 显式类型转换:显式类型转换,即强制类型转换。显式转换需要强制转换运算符,而且强制转换会造成数据丢失。

显式的类型转换示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace TypeConversionApplication
{
class ExplicitConversion
{
static void Main(string[] args)
{
double d = 5673.74;
int i;

// 强制转换 double 为 int
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}

//输出
5673

C#类型转换方法

C# 提供了下列内置的类型转换方法:

  • ToBoolean:转换为 bool
  • ToByte
  • Tochar
  • ToDateTime:转换为 日期-时间 结构
  • ToDecimal
  • ToDouble
  • Toint16
  • Toint32
  • Toint64
  • ToSbyte
  • ToSingle:把类型转换为小浮点数类型
  • ToString
  • ToType
  • ToUInt16
  • ToUInt32
  • ToUInt64

一个示例:

1
Console.WriteLine(i.ToString());

常量

静态常量const

静态常量(编译时常量)在编译时就确定了值,必须在声明时就进行初始化且之后不能进行更改,可在类和方法中定义。使用 const 关键字,定义一个常量的语法如下:

1
2
3
const <data_type> <constant_name> = value;
//例子
const double a = 3.14;

动态常量readonly

在运行时确定值,只能在声明时或构造函数中初始化,只能在类中定义。定义方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
class Program
{
readonly int a = 1;
readonly int b;
Program() // 构造函数
{
b = 2;
}
static void Main()
{
}
}

静态常量与动态常量的使用场景

在下面两种情况下,可以使用 const 常量:

  • 取值永久不变。比如圆周率、一天包含的小时数、地球的半径等。
  • 对程序性能要求非常苛刻

除此之外的其他情况都应该优先采用 readonly 常量。

C#运算符

Insist on writing original high-quality articles. Your support is my biggest motivation.