重载赋值运算符 [英] Overloading the assignment operator

查看:84
本文介绍了重载赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过,基本上在C#中,赋值运算符不会发生.这确实在加剧.我从一个FormCollection中获取值,当我获取它们时,这些值都是字符串.我正在编写如下代码:

I've read that basically in C# overloading the assignment operator isn't going to happen. This is truly aggravating. I'm picking up values from a FormCollection, which are all strings as I get them. I was writing loads of code like:

 

bool isTrue = Convert.ToBool(formCollection["someBoolean"]);

推荐答案

您需要研究隐式和显式运算符.  (对于您给出的示例,显式运算符可能是最好的),它们应该让您完全按照自己的意愿进行操作,即让编译器自动使用 尝试分配当前不匹配的类型时使用静态转换方法.
You'll want to look into the implicit and explicit operators.  (The explicit operator would probably be best for the examples you've given) They should let you do exactly what you want to do, namely have the compiler automatically use a static conversion method when you attempt to assign a type that doesn't currently match.


这篇关于重载赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆