自定义隐式转换和显式转换c#简单例子

自定义隐式转换和显式转换c#简单例子 (出自朱朱家园http://blog.csdn.net/zhgl7688

例子:对用户user中,用户名first name和last name进行转换成合成一个限定长度为10个字符新name。

自定义隐式转换:

namespace transduction
{
    public partial class transductionForm : Form
    {
        public transductionForm()
        {
            InitializeComponent();
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            User user = new User() { FirstName = textBox1.Text, LastName = textBox2.Text };
            LimitedName limitedName = user;//将user转换为limitedName
            string lName = limitedName;//将limitedName转换为字符串型
            listBox1.Items.Add(lName);
        }
    }
    class LimitedName
    {
        const int MaxNameLength = 10;//名字最长为10个字符
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value.Length < 10 ? value : value.Substring(0, 10); }
        }
        public static implicit operator LimitedName(User user)// public static implicit operator是必须的,名称LimitedName为目标类型,参数User为源数据。
        {
            LimitedName ln = new LimitedName();//建立目标实例
            ln.Name = user.FirstName + user.LastName;//将源数据赋于目标实例
            return ln;
        }
        public static implicit operator string(LimitedName ln)//
        {

            return ln.Name;//返回目标实例的数据。
        }
    }
    class User
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

自定义显式转换:

将上面程序中的用explicit替换implicit,

LimitedName limitedName =(LimitedName ) user;//在user增加显式转换类型

此文件由朱朱编写,转载请注明出自朱朱家园http://blog.csdn.net/zhgl7688

时间: 2024-07-31 14:27:02

自定义隐式转换和显式转换c#简单例子的相关文章

自己定义隐式转换和显式转换c#简单样例

自己定义隐式转换和显式转换c#简单样例 (出自朱朱家园http://blog.csdn.net/zhgl7688) 样例:对用户user中,usernamefirst name和last name进行转换成合成一个限定长度为10个字符新name. 自己定义隐式转换: namespace transduction { public partial class transductionForm : Form { public transductionForm() { InitializeCompon

ahjesus自定义隐式转换和显示转换

implicit    关键字用于声明隐式的用户定义类型转换运算符. 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换. 参考戳此 explicit    关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符. 参考戳此 有一点要注意的是,一个类中不能同时写显式和隐式声明,只能是其中一个. 但是如果声明了隐式的转换,那么其对应的显示转换也会自动提供. 如果声明了显式转换,其对应的隐式转换不会提供的.  ahjesus自定义隐式转换和显示转换

隐式转换和显式转换及强制转换的区别【转】

隐式转换和显式转换及强制转换的区别 string strType = "123"; object objType= (object)strType;//可以不要,隐式转换, 要的为显式转换 string strType2 = (string)objType; //必须要,显式转换 int intType = (int)strType; //错误,不能通过编译 int intType = (int)objType; //错误,能通过编译 int intType = Convert.ToI

隐式转换和显式转换及强制转换的区别

string strType = "123"; object objType= (object)strType;//可以不要,隐式转换, 要的为显式转换 string strType2 = (string)objType; //必须要,显式转换 int intType = (int)strType; //错误,不能通过编译 int intType = (int)objType; //错误,能通过编译 int intType = Convert.ToInt32(objType); //正

隐式转换和显式转换

C/C++对于数据类型的转换包括隐式转换和显式转换(强制类型转换). 一般来说,隐式转换包括以下几种情形: 1. 低精度与高精度混合运算,低精度会隐式转换成高精度类型. int a = 10; double b = 1.2; double c = a + b;//此时a会隐式转换成double类型进行运算. bool.char.short.int.long.float.double依次向上会发生隐式转换.bool类型向上转换时,false转换成0,true转换成1. [p.s.]有符号和无符号数

scala自定义隐式转换

Scala自定义隐式转换 一.编写隐式转换类 /** * Author Mr. Guo * Create 2019/4/20 - 17:40 */ object StringImprovments { implicit class StringImprove(s: String) { def increment = s.toString.map(c => (c + 1).toChar) } implicit class Intc(i: Int) { def xx = { Integer.pars

c# 强制转换, 隐式转换, 显式转换

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace 第二节课{    class Program    {        static void Main(string[] args)        {            decimal i = 4;            int a = 5;     

js 的隐式转换与显式转换

隐式转换 1.undefined与null相等,但不恒等(===) 2.一个是number一个是string时,会尝试将string转换为number 3.隐式转换将boolean转换为number,0或1 4.隐式转换将Object转换成number或string,取决于另外一个对比量的类型 5.对于0.空字符串的判断,建议使用 “===” . 6.“==”会对不同类型值进行类型转换再判断,“===”则不会.它会先判断两边的值类型,类型不匹配时直接为false. undefined == nu

利用implicit关键字做自定义类型隐式转换

在C#中,implicit关键字可以用来做自定义类型隐式转换.下面给个例子来说明. 先定义一个Point类,表示一个点: public class Point { public double X { get; set; } public double Y { get; set; } } 再在Point类中定义一个静态方法,用于由字符串隐式转换为Point类型: public class Point { public double X { get; set; } public double Y {