【问题标题】:getters and Setters in c# [closed]c# 中的 getter 和 Setter [关闭]
【发布时间】:2010-09-04 09:40:46
【问题描述】:

谁能告诉我这个控制台应用程序是做什么的?实际发生了什么?

另外,有错误。你能修复它们吗?

public class Program
{
    static void Main(string[] args)
    {
        Name n = new Name[5]; // error
        n[0] = new Name(0); //error
        n[0] = "hgfhf"; //is this possible in this program?
        string nam = n[0];
    }
}
public class Name
{
    private string[] name;
    private int size;
    public Name(int size)
    {
        this.name = new string[size];
        this.size = size;
    }
    public string this[int pos] // what does this mean?
    {
        get
        {
            return name[pos];
        }
        set
        {
            name[pos] = value;
        }
    }
}

【问题讨论】:

  • 请删除 txtspk,它只会让您难以阅读您想说的内容。
  • 如果您想使用 StackOverflow,请在您的问题上投入一些精力。我刚刚为您翻译;不要使用“文本”语音,至少尝试使用正确的拼写和标点符号。
  • 请详细说明您的问题。

标签: c#


【解决方案1】:

这是indexer property。它就像一个普通属性,但它允许您在其上使用 [] 语法:

public class Program
{
    static void Main(string[] args)
    {
        // Create a Name instance by calling it's constructor
        // capable of storing 1 string
        Name n = new Name(1);

        // Store a string in the name
        n[0] = "hgfhf";

        // Retrieve the stored string
        string nam = n[0];
    }
}

public class Name
{
    private string[] names;
    private int size;

    public Name(int size)
    {
        // initialize the names array for the given size
        this.names = new string[size];

        // store the size in a private field
        this.size = size;
    }

    /// <summary>
    /// Indexer property allowing to access the private names array
    /// given an index
    /// </summary>
    /// <param name="pos">The index to access the array</param>
    /// <returns>The value stored at the given position</returns>
    public string this[int pos]
    {
        get
        {
            return names[pos];
        }
        set
        {
            names[pos] = value;
        }
    }
}

【讨论】:

    【解决方案2】:
    Name n = new Name[5];//error
    

    变量n 是对Name 的单个实例的引用,因此您不能在其中放入数组。使用Name[] 作为变量的类型。

    n[0] = new Name(0);//error
    

    当您将变量设为数组时,此错误将消失。

    n[0] = "hgfhf";//is this possible in this program??
    

    不,这会尝试用字符串替换 Name 实例。如果要将字符串放在Name 实例中,请使用双索引,一个索引用于访问数组中的项目,一个索引用于访问Name 实例中的索引器属性:

    n[0][0] = "asdf";
    

    (但是,由于您已将 Name 实例的大小指定为零,这将导致 IndexOutOfRangeException。)

    public string this[int pos]//wt this means????
    

    这是索引器属性。它是一个带参数的属性,通常用于访问项目,就像对象是一个数组一样。

    【讨论】:

      【解决方案3】:
      Name n = new Name[5]; //this is declaration error
      

      --> 你需要声明一个类名数组。

      声明为:

      Name[] n = new Name[5];
      
      
      public string this[int pos] {}
      

      这意味着您已经定义了一个索引器属性,并且索引器允许像数组一样对 classstruct 的实例进行索引。

      现在您正在分配一个字符串值 (string nam = n[0]),因为该属性已定义,所以该值是正确的。

      【讨论】:

      • Ripper,点击编辑查看右侧帮助栏
      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      相关资源
      最近更新 更多