• 友链

  • 首页

  • 文章归档
h u a n b l o g
h u a n b l o g

欢

HI,Friend

04月
13
C#

类型转换关键字

发表于 2022-04-13 • 字数统计 3682 • 被 1,283 人看爆

is运算符

检查表达式的结果是否与给定的类型相匹配,转换成功返回true,否则false

实例

class Person
{
    public string Name = "Anonymous";
    public int Age = 25;
}

class Employee:Person { }

class Program
{
    static void Main(string[] args)
    {
        Employee bill = new Employee();
        Person p;
        if(bill is Person)
        {
            p = bill;
            Console.WriteLine($"Person Info:{p.Name},{p.Age}");
        }

        Console.ReadKey();
    }
}

分析

类型是匹配的返回true,就会进来执行

不能用于用户自定义转换

as运算符

和强制转换运算符类似,只是不抛出异常,转换失败返回null,不抛出异常
像一般的强制转换,转换失败,会抛出异常,中断程序,使用as运算符,转换失败只返回null

实例

class Person
{
    public string Name = "Anonymous";
    public int Age = 25;
}

class Employee:Person { }

class Program
{
    static void Main(string[] args)
    {
        Employee bill = new Employee();
        Person p = bill as Person;
        if(p != null)
        {
            p = bill;
            Console.WriteLine($"Person Info:{p.Name},{p.Age}");
        }

        Console.ReadKey();
    }
}

不能用于用户自定义转换

checked和unchecked

控制表达式的溢出检测上下文

运算符

语法

checked(表达式)
unchecked(表达式)

例

ushort sh = 2000;
byte sb;
sb = unchecked((byte)sh));
sb = checked((byte)sh);

语句

与运算符相同的功能,只不过不是圆括号内。控制的是一块代码中所有的转换,而不是单个表达式
可以任意嵌套

语法

checked{代码块}
unchecked{代码块}

例

ushort sh = 2000;
byte sb;
unchecked
{
    sb = (byte)sh;
    Console.WriteLine("sb:{0}",sb);
    checked{
        sb = (byte)sh;
        Console.WriteLine("sh:{0}",sh);
    }
}

implicit和explicit

用户定义类型可以定义从或到另一个类型的自定义隐式或显式转换
还需要加上关键字:operator

规则

  • 除了implicit和explicit关键字之外,隐式和显式转换的声明语法是一样的。
  • 需要public和static修饰符。

语法

public static operator TargetType(SourceType Identifer) {
    return ObjectOfTargetType;
}

例1

将Person类型对象转换为int

class Person
{
    public string Name;
    public int Age;

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
public static operator int(Person p) {
    return p.Age;
}

约束

  • 只可以为类和结构定义用户自定义转换。
  • 不能重定义标准隐式转换或显式转换。
  • 对于相同的源和目标类型,我们不能声明隐式转换和显式转换。

实例1

class Person
{
    public string Name;
    public int Age;

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public static implicit operator int(Person p)   //将person转换为int
    {
        return p.Age;
    }

    public static implicit operator Person(int i)   //将int转换为person
    {
        return new Person("Nemo", i);
    }
}

class Program
{
    static void Main(string[] args)
    {

        Person bill = new Person("bill", 25);
        int age = bill;     //把Person对象转换为int
        Console.WriteLine($"bill.Name:{bill.Name}, age:{age}");

        Person anon = 35;       //将int类型转换为person对象
        Console.WriteLine($"anon.Name:{anon.Name}, anon.Age:{anon.Age}");
        Console.ReadKey();
    }
}

结果

bill. Name:bill, age:25
anon. Name:Nemo, anon.Age:35

如果使用explicit运算符而不是implicit来转换,则需要使用强制转换表达式来进行转换

实例2

class Person
{
    public string Name;
    public int Age;

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public static explicit operator int(Person p)   //将person转换为int
    {
        return p.Age;
    }

    public static explicit operator Person(int i)   //将int转换为person
    {
        return new Person("Nemo", i);
    }
}

class Program
{
    static void Main(string[] args)
    {

        Person bill = new Person("bill", 25);
        int age = (int)bill;     //把Person对象转换为int
        Console.WriteLine($"bill.Name:{bill.Name}, age:{age}");
    }
}

分享到:
泛型
枚举
  • 文章目录
  • 站点概览
欢

网红 欢

你能抓到我么?

Email RSS
看爆 Top5
  • mac系统版本与Xcode版本有冲突 4,089次看爆
  • JAVA_HOME环境配置问题 3,739次看爆
  • AssetBundle使用 3,507次看爆
  • VSCode配置C++开发环境 3,262次看爆
  • Lua反射 3,138次看爆

Copyright © 2025 欢 粤ICP备2020105803号-1

由 Halo 强力驱动 · Theme by Sagiri · 站点地图