类型和变量(C#学习笔记02)

2022-10-14,,

类型变量

[c#类型和变量(原文参考官方教程)]

c#有两种类型:

1. 值类型

1. 简单类型

  1. 有符号的整型:sbyte、short、int、long
  2. 无符号的整型:byte、ushort、uint、ulong
  3. unicode 字符:char
  4. ieee 二进制浮点:float、double
  5. 高精度十进制浮点数:decimal
  6. 布尔:bool

2. 枚举类型

格式为 enum e {...} 的用户定义类型
每个枚举类型都有一个可以为任意整型数值类型的基础类型
与c++相同,枚举数可以使用初始值设定项来替代默认值,默认从0开始的整型数值

public class enumtest
{
    enum day { sun, mon, tue, wed, thu, fri, sat };

    static void main()
    {
        int x = (int)day.sun;
        int y = (int)day.fri;
        console.writeline("sun = {0}", x);
        console.writeline("fri = {0}", y);
    }
}
/* output:
   sun = 0
   fri = 5
*/

3. 结构类型

格式为 struct s {...} 的用户定义类型
struct是一种值类型,只要是用于封装小型数据
例如把“书”这个实体所包含的价格,书名和作者三个数据封装成结构体book

public struct book
{
    public decimal price;
    public string title;
    public string author;
}

4. 可为null的值类型

值为 null 的其他所有值类型的扩展
在标准类型下的扩展,是 system.nullable<t> 结构的实例
以int类型作为测试,int类型本身不能初始化为null类型

int? x = null;
if (x.hasvalue)
{
    console.writeline($"x is {x.value}");
}
else
{
    console.writeline("x does not have a value");
}

输出:

x does not have a value

2. 引用类型

1. 类类型

  1. 其他所有类型的最终基类:objectunicode
  2. 字符串:string
  3. 格式为 class c {...} 的用户定义类型

c#中,一个类只能从一个基类继承实现,但是一个类可以实现多个接口。

继承 示例
class classa { }
single class derivedclass: baseclass { }
无,实现两个接口 class implclass: iface1, iface2 { }
无,实现一个接口 class implderivedclass: baseclass, iface1 { }

类成员(包括嵌套的类)可以是 public、protected internal、protected、internal、private 或 private protected。 默认情况下成员为 private。

public:同一程序集中的任何其他代码或引用该程序集的其他程序集都可以访问该类型或成员。

private:只有同一类或结构中的代码可以访问该类型或成员。

protected:只有同一类或者从该类派生的类中的代码可以访问该类型或成员。

internal:同一程序集中的任何代码都可以访问该类型或成员,但其他程序集中的代码不可以。

protected internal:该类型或成员可由对其进行声明的程序集或另一程序集中的派生类中的任何代码访问。

private protected:只有在其声明程序集内,通过相同类中的代码或派生自该类的类型,才能访问类型或成员。

注:
被编译到同一个dll或exe中的程序就是处于同一个程序集中,在不同的dll或exe文件中的程序就是处于不同的程序集中。.net中的程序集就是一个编译器直接生成的dll或可执行的exe文件,包含程序集清单、元数据和msil等。是一个或者多个类型定义及资源文件的集合体。

下面的示例说明如何声明类字段、构造函数和方法。 该示例还说明如何实例化对象及如何打印实例数据。 本例声明了两个类。 第一个类 child 包含两个私有字段(name 和 age)、两个公共构造函数和一个公共方法。 第二个类 stringtest 用于包含 main。

class child
{
    private int age;
    private string name;

    // 无参数构造函数
    public child()
    {
        name = "n/a";
    }

    // 带参数的构造函数
    public child(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    // printing method:
    public void printchild()
    {
        console.writeline("{0}, {1} years old.", name, age);
    }
}

class stringtest
{
    static void main()
    {
        // create objects by using the new operator:
        child child1 = new child("craig", 11);
        child child2 = new child("sally", 10);

        // create an object using the default constructor:
        child child3 = new child();

        // display results:
        console.write("child #1: ");
        child1.printchild();
        console.write("child #2: ");
        child2.printchild();
        console.write("child #3: ");
        child3.printchild();
    }
}
/* output:
    child #1: craig, 11 years old.
    child #2: sally, 10 years old.
    child #3: n/a, 0 years old.
*/

2. 接口类型

格式为 interface i {...} 的用户定义类型
接口只包含方法、属性、事件或索引器的签名。 实现接口的类或结构必须实现接口定义中指定的接口成员。
实例:

interface isampleinterface       //只定义声明
{
    void samplemethod();
}

class implementationclass : isampleinterface
{
    // explicit interface member implementation: (具体的方法实现在类中实现)
    void isampleinterface.samplemethod()
    {
        // method implementation.(方法代码)
    }

    static void main()
    {
        // declare an interface instance.
        isampleinterface obj = new implementationclass();

        // call the member.
        obj.samplemethod();
    }
}

3. 数组类型

一维和多维,例如 int[] 和 int[,]
可以将同一类型的多个变量存储在一个数组数据结构中。 通过指定数组的元素类型来声明数组。

class testarraysclass
{
    static void main()
    {
        // declare a single-dimensional array. 
        int[] array1 = new int[5];

        // declare and set array element values.
        int[] array2 = new int[] { 1, 3, 5, 7, 9 };

        // alternative syntax.
        int[] array3 = { 1, 2, 3, 4, 5, 6 };

        // declare a two dimensional array.
        int[,] multidimensionalarray1 = new int[2, 3];

        // declare and set array element values.
        int[,] multidimensionalarray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

        // declare a jagged array.
        int[][] jaggedarray = new int[6][];

        // set the values of the first array in the jagged array structure.
        jaggedarray[0] = new int[4] { 1, 2, 3, 4 };
    }
}

4. 委托类型

格式为 delegate int d(...) 的用户定义类型
具体参考:事件与委托学习笔记03

https://www.cnblogs.com/asahilikka/p/11644393.html

《类型和变量(C#学习笔记02).doc》

下载本文的Word格式文档,以方便收藏与打印。