C# 可附加的索引器显式实现

C# 可附加的索引器显式实现,c#,interface,indexer,C#,Interface,Indexer,我给自己写了一个索引器,它可以工作,但一旦我实现了“IFormatable”显式,它就不再工作了 为什么它不起作用?如何明确使用它 这就是我所尝试的: /// <summary> /// Example of Coordinates for 3 dimensions. /// </summary> public class Coordinates:IFormattable { #region Constructor public Coordinates(i

我给自己写了一个索引器,它可以工作,但一旦我实现了“IFormatable”显式,它就不再工作了

为什么它不起作用?如何明确使用它

这就是我所尝试的:

/// <summary>
/// Example of Coordinates for 3 dimensions.
/// </summary>
public class Coordinates:IFormattable
{
    #region Constructor
    public Coordinates(int x, int y, int z)
    {
        _x = x;
        _y = y;
        _z = z;
        _length = 3;
    }
    #endregion
    #region IFormattable implemented
    /// <summary>
    /// Formating custom string
    /// </summary>
    /// <param name="format">Format("A"-all value,...,"H"-)</param>
    /// <param name="formatProvider">Different culture</param>
    /// <returns></returns>
    public string IFormattable.ToString(string format, IFormatProvider formatProvider)
    {
        if (format==null)
        {
            return ToString(); //Fallbasck
        }
        else
        {
            switch (format.ToUpper())//gross, kleinschreibung egal
            {
                case "A": return ToString();
                case "X": return _x.ToString();
                case "Y": return _y.ToString();
                case "Z": return _z.ToString();
                case "H": return String.Format("{0:h}", ToString());
                default:
                    throw new FormatException(String.Format("Format {0} is not defined.", format));
            }
        }
    }

    public string ToString(string format)
    {
        return ToString(format, null); //Error occurs here
    }

    #endregion
    #region overWriteToString
    /// <summary>
    /// will be called implicit
    /// </summary>
    /// <returns>[x,y,z]</returns>
    public override string ToString()
    {
        return String.Format("[{0},{1},{2}]",_x,_y,_z);
    }
    #endregion
    #region Properties
    private int _x;

    public int X
    {
        get { return _x; }
        set { _x = value; }
    }

    private int _y;

    public int Y
    {
        get { return _y; }
        set { _y = value; }
    }

    private int _z;

    public int Z
    {
        get { return _z; }
        set { _z = value; }
    }

    private int _length;

    public int Length
    {
        get { return _length; }
    }

    #endregion
    #region Indexer
    /// <summary>
    /// My own indexer for x,y,z
    /// </summary>
    /// <param name="val"></param>
    /// <returns></returns>
    public int this[int val]
    {
        get
        {

            switch (val)
            {
                case 0: return _x; ;
                case 1: return _y; ;
                case 2: return _z; ;
                default:
                    throw new IndexOutOfRangeException("Indexer not avaiable.");
            }

        }
        //kann auch setter haben
    }

    #endregion

}

通过显式实现
ToString(字符串格式,IFormatProvider formatProvider)
,您只能通过编译时类型为
IFormattable
的引用调用它

您可以使用

return ((IFormattable)this).ToString(format, null);
或:

我可能会在代码中添加一条注释,解释您为什么要强制转换,因为直到您注意到该方法显式实现了
IFormattable.ToString
,这才是显而易见的


顺便说一句,你有什么理由想显式地实现它吗?它感觉像是应该可以访问的东西-您应该使用传入的
IFormatProvider

我只做了明确的说明,从“ToString(…)”的来源可以看出,如果您有不同的接口,并且有相同的方法,我知道实际上明确的实现是发明的,但我想不出有什么不同的方法来表达清楚,“ToString(…)”comes@user254197:您所说的“ToString来自何处”是什么意思?我有3个“ToString()”-方法,我希望看到其中一个来自接口“IFormattable.ToString(…)”,以便向所有人明确说明它们中的哪一个(ToString())是我的,哪些是基于.Net的。@user254197:为什么有人会在意?您可以记录实现接口的是哪一个,但是使用显式接口实现只会使它更难在其他地方使用…您是对的,我想如果我在您第一眼看到它之前就这样做…我知道已经有一段时间了,但是如果您仅为
public int Z{get使用带有支持字段的属性{return{uz;}set{uz=value;}}
返回值,使用自动属性,如
public int z{get;set;}
return ((IFormattable)this).ToString(format, null);
IFormattable formattable = this;
return formattable.ToString(format, null);