【C#学习笔记】值类型(1)

在这里插入图片描述

虽然拥有编程基础的人可以很快地上手C#,但是依然需要学习C#的特性和基础。本系列是本人学习C#的笔记,完全按照微软官方文档编写,但是不适合没有编程基础的人。


.NET 体系结构

C# 程序在 .NET 上运行,而 .NET 是名为公共语言运行时 (CLR) 的虚执行系统和一组类库。 CLR 是 Microsoft
对公共语言基础结构 (CLI) 国际标准的实现。 CLI 是创建执行和开发环境的基础,语言和库可以在其中无缝地协同工作。

简单来说,C#是在.NET上运行的,.NET是一个开发框架,框架内各种不同语言的代码可以互相交互,而C#中的数据类型定义遵循CTS以转化为CLR通用的数据类型。.NET提供了一个叫做CLR的虚拟机,CLR是基于CLI规范实现的虚拟运行时环境而CLI是一个跨语言开发的规范它定义了用于跨平台编译的中间语言ILIL 代码和资源(如位图和字符串)存储在扩展名通常为 .dll 的程序集中,执行C#的时候通过在CLR执行实时 (JIT) 编译将IL转化为本机指令集从而实现跨平台


Hello,World

using System;

class Hello
{
    
    
    static void Main()
    {
    
    
        Console.WriteLine("Hello, World");
    }
}

上述是C#的 Hello,World。看起来就像是C++和Java的合体。

使用命名空间时使用的是using关键字+命名空间名,随后我们定义了一个类,在其中定义了Main()方法,Main 方法使用 static 修饰符进行声明。 实例方法可以使用关键字 this 引用特定的封闭对象实例,而静态方法则可以在不引用特定对象的情况下运行。 按照约定,Main 静态方法是 C# 程序的入口点。

好的,现在你已经学会C#了


类型和变量(重要)

C#中存在两大类型,一种是值类型,一种是引用类型。对这两种类型的理解是十分重要的。

值类型的变量直接包含它们的数据。 引用类型的变量存储对数据(称为“Object”)的引用。 对于引用类型,两个变量可以引用同一个对象;对一个变量执行的运算可能会影响另一个变量引用的对象。 借助值类型,每个变量都有自己的数据副本;因此,对一个变量执行的运算不会影响另一个变量(ref 和 out 参数变量除外)。

什么是值类型和引用类型?

原文链接:C# 如何避免装箱和拆箱操作

  • 什么是值类型:

所有的结构都是抽象类型System.ValueType的直接派生类,而System.ValueType本身又是直接从System.Object派生的。根据定义所知,所有的值类型都必须从System.ValueType派生,所有的枚举都从System.Enum抽象类派生,而后者又从System.ValueType派生。

所有的值类型都是隐式密封的(sealed),目的是防止其他任何类型从值类型进行派生。

  • 什么是引用类型:

在c#中所有的类都是引用类型,包括接口。

所以C#语言中的数据类型,实际上是从.NET的抽象基类中所派生的。

值类型

简单类型

  • 有符号整型:sbyte、short、int、long
  • 无符号整型:byte、ushort、uint、ulong
  • Unicode 字符:char,表示 UTF-16 代码单元
  • IEEE 二进制浮点:float、double
  • 高精度十进制浮点数:decimal
  • 布尔值:bool,表示布尔值(true 或 false)

枚举类型

  • enum E {…} 格式的用户定义类型。 enum 类型是一种包含已命名常量的独特类型。 每个 enum 类型都有一个基础类型(必须是八种整型类型之一)。 enum 类型的值集与基础类型的值集相同。

结构类型

  • 格式为 struct S {…} 的用户定义类型
  • 可以为 null 的值类型
    值为 null 的其他所有值类型的扩展
  • 元组值类型
    格式为 (T1, T2, …) 的用户定义类型

简单类型

简单类型包括整数型,浮点型,字符,布尔。也就是一些编程语言中的常用类型。

有符号整型:sbyte、short、int、long
这些都是很基础的,可以看官方文档,其中特殊的类型是sbyte,byte,nint,nuint。后者是C# 11加入的,用于获取不同操作系统下本机正数范围大小:
对于 nint:Int32.MinValue 到 Int32.MaxValue。
对于 nuint:UInt32.MinValue 到 UInt32.MaxValue。

sbyte和byte的范围大小是1字节,sbyte包括了负数范围,但sbyte不符合CLR,所以我们只关注byte。byte与其他整数类型不同的是,直接赋值它一个ASCII字符,是不存在字符类型和byte类型的隐式和显式转换的,所以不可行,例如byte b='a'

对于整数型的数据,通常当数据溢出超过范围之后会改变(当然这和字节中的比特位变化相关)。例如byte型的255+1=0,sbyte的127+1= -128(特殊的,寄存器用00000000表示0,而10000000表示-128)

在这里插入图片描述浮点数值类型之间只有一种隐式转换:从 float 到 double。 但是,可以使用显式强制转换将任何浮点类型转换为任何其他浮点类型。

在C#中,我们还可以用.NET类型定义或者跨语言,例如int a=1System.Int32 a=1是一致的。
在为整数赋值时可以使用十进制,十六进制和二进制,十进制就是正常的数字,十六进制以0X开头,二进制以0B开头。我们在赋值时使用十进制也可以以_作为分隔符,例如int a=3000int a =3_000是等价的。

最后,大部分数字类型可以带有后缀字符,例如0.1f用f代表float,21L用L代表long。


枚举类型

枚举类型 是由基础整型数值类型的一组命名常量定义的值类型。 若要定义枚举类型,请使用 enum 关键字并指定枚举成员 的名称:

enum Season
{
    
    
    Spring,
    Summer,
    Autumn,
    Winter
}

一个枚举类型的变量的值只能是内部几种值之一。例如Season S1 =Season.Spring;。直接打印会打印出枚举的成员名。但实质上枚举类型内的值对应着默认的整数,若无定义就从0开始从上往下依次递增,有定义则是定义的整数值。而使用枚举类型名也可以对整数值实现显式转换。

print(S1) // Spring
print((int)S1) // 0
var b = (Season)1;
Console.WriteLine(b);  // output: Summer

枚举另一种表示方法是接受二进制表示,并可以用逻辑运算符按位运算,这是一般的用法。
而当我们使用[Flags]指示可以将枚举指示为位域,枚举中的每个选项将称为一个位字段,此时通过逻辑运算符可以合并选项或交叉组合选项:

[Flags]
public enum Days
{
    
    
    None      = 0b_0000_0000,  // 0
    Monday    = 0b_0000_0001,  // 1
    Tuesday   = 0b_0000_0010,  // 2
    Wednesday = 0b_0000_0100,  // 4
    Thursday  = 0b_0000_1000,  // 8
    Friday    = 0b_0001_0000,  // 16
    Saturday  = 0b_0010_0000,  // 32
    Sunday    = 0b_0100_0000,  // 64
    Weekend   = Saturday | Sunday
}
public class FlagsEnumExample
{
    
    
    public static void Main()
    {
    
    
        Days meetingDays = Days.Monday | Days.Wednesday | Days.Friday;
        Console.WriteLine(meetingDays);
        // Output:
        // Monday, Wednesday, Friday

        Days workingFromHomeDays = Days.Thursday | Days.Friday;
        Console.WriteLine($"Join a meeting by phone on {
      
      meetingDays & workingFromHomeDays}");
        // Output:
        // Join a meeting by phone on Friday

        bool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday;
        Console.WriteLine($"Is there a meeting on Tuesday: {
      
      isMeetingOnTuesday}");
        // Output:
        // Is there a meeting on Tuesday: False

        var a = (Days)37;
        Console.WriteLine(a);
        // Output:
        // Monday, Wednesday, Saturday
    }
}

扩展方法

在枚举类型中,通常我们不能定义方法,但是可以通过定义拓展方法来为现有类型“添加方法”而无需修改类型。而扩展方法就是用于对值类型的扩展的,例如想要为所有的string添加一个去掉空格的处理方法,不必定义一个类然后在里面定义方法和string变量。可以直接用扩展方法,但是按扩展方法必须满足的条件:

  • 必须要静态类中的静态方法
  • 第一个参数的类型是要扩展的类型,并且需要添加this关键字以标识其为扩展方法

通常,只在不得已的情况下才实现扩展方法,并谨慎的实现。扩展方法不能通过类名调用,而直接使用类型来调用。

namespace ExtensionMethods
{
    
    
    public static class MyExtensions
    {
    
    
        public static int WordCount(this string str)
        {
    
    
            return str.Split(new char[] {
    
     ' ', '.', '?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}
// 上述扩展方法定义在了一个命名空间中的类中的一个方法,定义在命名空间中在调用时会更安全
// 在静态类中定义静态方法,并以添加this关键字标识类型传入
using ExtensionMethods;
string s = "Hello Extension Methods";
int i = s.WordCount();

在下例子中官方展示了一个绑定接口的拓展方法,任何继承这个接口的类,当其内部没有定义与拓展方法同名的方法时,就可以调用这个拓展方法。

// Define an interface named IMyInterface.
namespace DefineIMyInterface
{
    
    
    using System;

    public interface IMyInterface
    {
    
    
        // Any class that implements IMyInterface must define a method
        // that matches the following signature.
        void MethodB();
    }
}

// Define extension methods for IMyInterface.
namespace Extensions
{
    
    
    using System;
    using DefineIMyInterface;

    // The following extension methods can be accessed by instances of any
    // class that implements IMyInterface.
    public static class Extension
    {
    
    
        public static void MethodA(this IMyInterface myInterface, int i)
        {
    
    
            Console.WriteLine
                ("Extension.MethodA(this IMyInterface myInterface, int i)");
        }

        public static void MethodA(this IMyInterface myInterface, string s)
        {
    
    
            Console.WriteLine
                ("Extension.MethodA(this IMyInterface myInterface, string s)");
        }

        // This method is never called in ExtensionMethodsDemo1, because each
        // of the three classes A, B, and C implements a method named MethodB
        // that has a matching signature.
        public static void MethodB(this IMyInterface myInterface)
        {
    
    
            Console.WriteLine
                ("Extension.MethodB(this IMyInterface myInterface)");
        }
    }
}

// Define three classes that implement IMyInterface, and then use them to test
// the extension methods.
namespace ExtensionMethodsDemo1
{
    
    
    using System;
    using Extensions;
    using DefineIMyInterface;

    class A : IMyInterface
    {
    
    
        public void MethodB() {
    
     Console.WriteLine("A.MethodB()"); }
    }

    class B : IMyInterface
    {
    
    
        public void MethodB() {
    
     Console.WriteLine("B.MethodB()"); }
        public void MethodA(int i) {
    
     Console.WriteLine("B.MethodA(int i)"); }
    }

    class C : IMyInterface
    {
    
    
        public void MethodB() {
    
     Console.WriteLine("C.MethodB()"); }
        public void MethodA(object obj)
        {
    
    
            Console.WriteLine("C.MethodA(object obj)");
        }
    }

    class ExtMethodDemo
    {
    
    
        static void Main(string[] args)
        {
    
    
            // Declare an instance of class A, class B, and class C.
            A a = new A();
            B b = new B();
            C c = new C();

            // For a, b, and c, call the following methods:
            //      -- MethodA with an int argument
            //      -- MethodA with a string argument
            //      -- MethodB with no argument.

            // A contains no MethodA, so each call to MethodA resolves to
            // the extension method that has a matching signature.
            a.MethodA(1);           // Extension.MethodA(IMyInterface, int)
            a.MethodA("hello");     // Extension.MethodA(IMyInterface, string)

            // A has a method that matches the signature of the following call
            // to MethodB.
            a.MethodB();            // A.MethodB()

            // B has methods that match the signatures of the following
            // method calls.
            b.MethodA(1);           // B.MethodA(int)
            b.MethodB();            // B.MethodB()

            // B has no matching method for the following call, but
            // class Extension does.
            b.MethodA("hello");     // Extension.MethodA(IMyInterface, string)

            // C contains an instance method that matches each of the following
            // method calls.
            c.MethodA(1);           // C.MethodA(object)
            c.MethodA("hello");     // C.MethodA(object)
            c.MethodB();            // C.MethodB()
        }
    }
}
/* Output:
    Extension.MethodA(this IMyInterface myInterface, int i)
    Extension.MethodA(this IMyInterface myInterface, string s)
    A.MethodB()
    B.MethodA(int i)
    B.MethodB()
    Extension.MethodA(this IMyInterface myInterface, string s)
    C.MethodA(object obj)
    C.MethodA(object obj)
    C.MethodB()
 */

猜你喜欢

转载自blog.csdn.net/milu_ELK/article/details/132057477
今日推荐