Indexer显式实现了IFormattable

时间:2015-08-07 09:55:40

标签: c# interface indexer

我自己写了一个索引器,但是只要我实现" 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

}

我正在详细学习C#。

此处发生错误:

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

1 个答案:

答案 0 :(得分:1)

通过显式实现ToString(string format, IFormatProvider formatProvider),您只能通过编译时类型为IFormattable的引用来调用它。

您可以使用this

来实现
return ((IFormattable)this).ToString(format, null);

或者:

IFormattable formattable = this;
return formattable.ToString(format, null);

我可能会在解释为什么的代码中添加注释,因为在您注意到该方法明确实现IFormattable.ToString之前,这一点并不明显。

想要明确地实现它是否有任何理由,顺便说一下?感觉就像应该可以访问的东西 - 你应该使用传入的IFormatProvider