有没有更好的方法在C#3.0中编写此C#代码行? [英] Is there a better way to write this line of C# code in C#3.0?

查看:68
本文介绍了有没有更好的方法在C#3.0中编写此C#代码行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属性声明如下:

I have a property declared as follows:

public decimal? MyProperty { get; set; }

我需要将此值作为字符串传递给另一个方法,所以我唯一的方式是这样做如下:

I am needing to pass this value to another method as a string and so the only way I see to do so is as follows:

MyProperty == null ? null : MyProperty.ToString()

当您传递许多类似的属性时,这看起来很混乱

This looks very messy when you have a number of similar properties being passed into a method.

有人知道是否存在更好,更简洁的编写方法吗?

Does anyone know if there is a better and more concise way of writing this?

哦,如果有人可以想到这个问题的更合适的标题,请随时进行更改...

Oh, and if anyone can think of a more appropriate title to this question please feel free to change it...

推荐答案

您可以使用 Nullable< T> .ToString() 覆盖 ...

You can use the Nullable<T>.ToString() override ...

var s = MyProperty.ToString(); // returns "" if MyProperty is null

这篇关于有没有更好的方法在C#3.0中编写此C#代码行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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