用户定义的显式和隐式转换运算符
参考代码
用户定义的显式和隐式转换运算符 - 提供对不同类型的转换 | Microsoft Learn
代码例程
using System;
public readonly struct Digit
{
private readonly byte digit;
public Digit(byte digit)
{
if (digit > 9)
{
throw new ArgumentOutOfRangeException(nameof(digit), "Digit cannot be greater than nine.");
}
this.digit = digit;
}
public static implicit operator byte(Digit d) => d.digit;
public static explicit operator Digit(byte b) => new Digit(b);
public override string ToString() => $"{digit}";
}
public static class UserDefinedConversions
{
public static void Main()
{
var d = new Digit(7);
byte number = d;
Console.WriteLine(number); // output: 7
Digit digit = (Digit)number;
Console.WriteLine(digit); // output: 7
}
}
自实现 隐式运算符转换例程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Person
{
public string Name { get; set; }
public int age { get; set; }
public Person(string name, int age)
{
Name = name;
this.age = age;
}
/// <summary>
/// 自定义隐式转换
/// </summary>
/// <param name="p"></param>
public static implicit operator string (Person p)
{
return p.Name;
}
}
internal class Program
{
static void Main(string[] args)
{
Person p1 = new Person("张三",20);
Person p2 = new Person("李四", 30);
string username = p1 + p2; //自定义对象隐式转换
Console.WriteLine(username);
Console.ReadLine();
}
}
}
运行结果
上面当然也可以适用于年龄,只需要修改返回值即可。
自实现 显式运算符转换例程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Person
{
public string Name { get; set; }
public int age { get; set; }
public Person(string name, int age)
{
Name = name;
this.age = age;
}
/// <summary>
/// 自定义隐式转换
/// </summary>
/// <param name="p"></param>
//public static implicit operator string(Person p)
//{
// return p.Name;
//}
//public static implicit operator Person(string name)
//{
// return new Person(name, 0);
//}
///运算符重载
public static string operator +(Person p1, Person p2)
{
return p1.Name + p2.Name;
}
}
internal class Program
{
static void Main(string[] args)
{
Person p1 = new Person("张三", 20);
Person p2 = new Person("李四", 30);
//string username = p1 + p2; //自定义对象隐式转换
//Console.WriteLine(username);
//Person p3 = "王五"; //强转,与视频中不同的时前面没有加括号
//Person p3a = (Person)"赵六"; //视频中加了括号强转
//Console.WriteLine(p3.Name + " " + p3.age.ToString());
//Console.WriteLine(p3a.Name + " " + p3a.age.ToString());
string username = p1 + p2; //自定义对象 + 重载
Console.WriteLine(username);
Console.ReadLine();
}
}
}
运行结果
与上面的相同,注意这里时显式运算符重载。
强制类型转换
自定义对象强转转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Person
{
public string Name { get; set; }
public int age { get; set; }
public Person(string name, int age)
{
Name = name;
this.age = age;
}
/// <summary>
/// 自定义隐式转换
/// </summary>
/// <param name="p"></param>
//public static implicit operator string(Person p)
//{
// return p.Name;
//}
public static implicit operator Person(string name)
{
return new Person(name, 0);
}
}
internal class Program
{
static void Main(string[] args)
{
Person p1 = new Person("张三", 20);
Person p2 = new Person("李四", 30);
//string username = p1 + p2; //自定义对象隐式转换
//Console.WriteLine(username);
Person p3 = "王五"; //强转,与视频中不同的时前面没有加括号
Person p3a = (Person)"赵六"; //视频中加了括号强转
Console.WriteLine(p3.Name + " " + p3.age.ToString());
Console.WriteLine(p3a.Name + " " + p3a.age.ToString());
Console.ReadLine();
}
}
}
运行结果(注意没有初始化年龄,使用默认)
+运算符重载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
class Person
{
public string Name { get; set; }
public int age { get; set; }
public Person(string name, int age)
{
Name = name;
this.age = age;
}
/// <summary>
/// 自定义隐式转换
/// </summary>
/// <param name="p"></param>
//public static implicit operator string(Person p)
//{
// return p.Name;
//}
//public static implicit operator Person(string name)
//{
// return new Person(name, 0);
//}
///运算符重载
public static string operator +(Person p1, Person p2)
{
return p1.Name + p2.Name;
}
public static Person operator ++(Person p)
{
p.age++;
return p;
}
}
internal class Program
{
static void Main(string[] args)
{
Person p1 = new Person("张三", 20);
Person p2 = new Person("李四", 30);
//string username = p1 + p2; //自定义对象隐式转换
//Console.WriteLine(username);
//Person p3 = "王五"; //强转,与视频中不同的时前面没有加括号
//Person p3a = (Person)"赵六"; //视频中加了括号强转
//Console.WriteLine(p3.Name + " " + p3.age.ToString());
//Console.WriteLine(p3a.Name + " " + p3a.age.ToString());
//string username = p1 + p2; //自定义对象 + 重载
//Console.WriteLine(username);
p1++;
Console.WriteLine(p1.age.ToString());
Console.ReadLine();
}
}
}
运行结果(略)
附一个微软的例子
public readonly struct Fraction
{
private readonly int num;
private readonly int den;
public Fraction(int numerator, int denominator)
{
if (denominator == 0)
{
throw new ArgumentException("Denominator cannot be zero.", nameof(denominator));
}
num = numerator;
den = denominator;
}
public static Fraction operator +(Fraction a) => a;
public static Fraction operator -(Fraction a) => new Fraction(-a.num, a.den);
public static Fraction operator +(Fraction a, Fraction b)
=> new Fraction(a.num * b.den + b.num * a.den, a.den * b.den);
public static Fraction operator -(Fraction a, Fraction b)
=> a + (-b);
public static Fraction operator *(Fraction a, Fraction b)
=> new Fraction(a.num * b.num, a.den * b.den);
public static Fraction operator /(Fraction a, Fraction b)
{
if (b.num == 0)
{
throw new DivideByZeroException();
}
return new Fraction(a.num * b.den, a.den * b.num);
}
public override string ToString() => $"{num} / {den}";
}
public static class OperatorOverloading
{
public static void Main()
{
var a = new Fraction(5, 4);
var b = new Fraction(1, 2);
Console.WriteLine(-a); // output: -5 / 4
Console.WriteLine(a + b); // output: 14 / 8
Console.WriteLine(a - b); // output: 6 / 8
Console.WriteLine(a * b); // output: 5 / 8
Console.WriteLine(a / b); // output: 10 / 4
}
}
参考讲解实例,老师讲得非常好,希望大家多多关注ub主
c shape c# 自定义隐式转换与运算符重载_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1rg411A7QU/?spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=e821a225c7ba4a7b85e5aa6d013ac92e
特此记录
anlog
2023年2月11日