小数点后舍入扩展时出错-无法通过实例引用进行访问;用类型名称来代替它 [英] Error with rounding extension on decimal - cannot be accessed with an instance reference; qualify it with a type name instead
本文介绍了小数点后舍入扩展时出错-无法通过实例引用进行访问;用类型名称来代替它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用扩展方法很多次了,还没有遇到这个问题.任何人都知道为什么会引发错误吗?
I've used extension methods numerous times, and haven't run into this issue. Anyone have any ideas why this is throwing an error?
/// <summary>
/// Rounds the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="decimals">The decimals.</param>
/// <returns></returns>
public static decimal Round (this decimal value, int decimals)
{
return Math.Round(value, decimals);
}
用法:
decimal newAmount = decimal.Parse("3.33333333333434343434");
this.rtbAmount.Text = newAmount.Round(3).ToString();
newAmount.Round(3)引发编译器错误:
newAmount.Round(3) is throwing the compiler error:
Error 1 Member 'decimal.Round(decimal)' cannot be accessed with an instance reference; qualify it with a type name instead
推荐答案
The conflict here is a conflict between your extension method and decimal.Round
. The simplest fix here, as already discovered, is to use a different name. Methods on the type always take precedence over extension methods, even to the point of conflicting with static
methods.
这篇关于小数点后舍入扩展时出错-无法通过实例引用进行访问;用类型名称来代替它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文