实现Interface类型的字符串数组属性

时间:2015-10-14 13:02:24

标签: c#

所以在我的界面中我写了这个:

public interface IFileExport
{
    string[] FileHeaders { get; set; }
}

在我班上这样的事情:

public class AdditionsFileExport: IFileExport
{
    public string[] FileHeaders {
        get
        {
            return new string["wwwe", "sdd", "e3rs"];
        }
        set { FileHeaders = value; } }
}

但我得到两个错误:

  1. 无法将类型字符串隐式转换为int。 (在GET上     部分)
  2. 函数在所有路径上递归。 (在SET部分)
  3. 我做了什么错误的事情以及正确的方法是什么?

1 个答案:

答案 0 :(得分:3)

您的声明或数组在语法上是不正确的。

        _socket = io.connect(url);
        _socket.on('testemit', function (message) {
            console.log('testemit');
            console.log(message);
        });

如果您需要readonly属性,请移除setter并使用public class AdditionsFileExport: IFileExport { // "wwwe", "sdd", "e3rs" will be default values private string[] fileHeaders = new[] { "wwwe", "sdd", "e3rs" }; public string[] FileHeaders { get { return fileHeaders; } set { fileHeaders = value; } } } 关键字声明fileHeaders

请注意:

readonly

不是一种可行的方法,因为每次调用public class AdditionsFileExport: IFileExport { public string[] FileHeaders { get { return new[] { "wwwe", "sdd", "e3rs" }; } } } 时,都会创建新的数组实例。因此,这个断言将失败:

FileHeaders

另请注意,该数组通常不是一个好的返回类型,因为您无法更改属性实现,例如,这样:

var foo = new AdditionsFileExport();
Debug.Assert(foo.FileHeaders == foo.FileHeaders);

public class AdditionsFileExport: IFileExport { private readonly fileHeaders = new List<string> { "wwwe", "sdd", "e3rs" }; public string[] FileHeaders { // won't compile, because List<> isn't array get { return fileHeaders; } } } 在这里会更好。