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

网友投稿 578 2022-09-13

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

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

类型和变量

[C#类型和变量(参考教程)]https://xin3721.com/eschool/csharpxin3721/

C#有两种类型:

1. 值类型

1. 简单类型

有符号的整型:sbyte、short、int、long

无符号的整型:byte、ushort、uint、ulong

Unicode 字符:char

IEEE 二进制浮点:float、double

高精度十进制浮点数:decimal

布尔: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是一种值类型,只要是用于封装小型数据

public struct Book

{

public decimal price;

public string title;

public string author;

}

4. 可为null的值类型

值为 null 的其他所有值类型的扩展

在标准类型下的扩展,是 System.Nullable 结构的实例

以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. 类类型

其他所有类型的最终基类:objectUnicode

字符串:string

格式为 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文件中的程序就是处于不同的程序集中。-中的程序集就是一个编译器直接生成的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://xin3721.com/eschool/csharpxin3721/

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Windows8开发指南(8)在Visual Studio11编译器的安全性增强-下一代Visual C++2012的新特性
下一篇:Windows8开发指南(14)开发第一个基于Windows8的Windows azure云计算程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~