如何在c#中获取和设置属性 [英] how property get and set work in c#

查看:63
本文介绍了如何在c#中获取和设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者。这是msdn代码的一部分代码,最后我添加了一个USState类。



请解释代码的这一部分,因为它实例化一个USState类的新对象和

传递参数,如何在这个序列中调用get和set welsors



 USStates.Add( new  USState( < span class =code-string> Alabama,  AL)); 



调用USState构造函数并传递此参数。

================= =============================

  private   string  myShortName ;;  //    this.myShortName  之间的关系
private string myLongName;

public USState( string strLongName, string strShortName)
{

this .myShortName = strShortName; // this.myShortName之间的关系
this .myLongName = strLongName;
}



带有获取和设置加法器的this.myShortName评论之间的关系是什么

=============================================== ===============

  //  使用数组作为DataSource填充列表框。 
ArrayList USStates = new ArrayList();
USStates.Add( new USState( Alabama AL));
USStates.Add( new USState( 华盛顿 WA));
USStates.Add( new USState( 西弗吉尼亚州 WV));
USStates.Add( new USState( 威斯康星州 WI));
USStates.Add( new USState( 怀俄明州 WY));
ListBox1.DataSource = USStates;

// 将长名称设置为要显示的属性和短
// name作为选择行时要返回的值。这里
// 这些是属性;如果我们绑定到数据库表或
// 查询,这些可能是列名。
ListBox1.DisplayMember = LongName;
ListBox1.ValueMember = ShortName;

public class USState
{
private string myShortName;
private string myLongName;

public USState( string strLongName, string strShortName)
{

this .myShortName = strShortName;
this .myLongName = strLongName;
}

public string ShortName
{
get
{
return myShortName;
}
}

public string LongName
{

get
{
return myLongName;
}
}

}
}

解决方案

吸气剂是当您读取属性的值时调用:

 USState state =  new  USState( 华盛顿  WA); 

// ...

< span class =code-keyword> string whereAmI = state.LongName; // 计算值调用getter



为某个属性赋值时会调用setter。你没有定义任何setter。



整个属性的概念是可能会在操作背后引入一些副作用,因为在语法上看起来与阅读操作完全相同或分配字段值。







请另请参阅austinbox的评论。



您的支持字段是多余的。在较新的语法中,引入了自动实现的支持字段。这就是你所需要的:



  public   class  USState 
{

public USState( string longName, string shortName)
{
this 。 ShortName = shortName;
this .LongName = longName;
}

public string ShortName {获得; } // 暗示支持字段
public string LongName { get ; } // 暗示支持字段

}
}





-SA


此代码中有两部分。



1)两个属性中都没有设置访问器。这两个变量在构造函数中赋值。



2)然后从类外部访问这些属性,并在绑定期间将这些值返回给调用类。 />

 ListBox1.DisplayMember =   LongName; 
ListBox1.ValueMember = ShortName;


I am a beginner.This is the code a part of code from msdn,there is a class USState which i have add at the end.

Please Explain this part of code as it instance a new object of class USState and
passing the parameters,How does the get and set accesors get invoked in this sequence
.

USStates.Add(new USState("Alabama", "AL"));


USState constructor gets invoked and this parameters are passed.
==============================================

private string myShortName;;//relationship between this.myShortName 
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;//relationship between this.myShortName 
            this.myLongName = strLongName;
        }


what is the // relationship between this.myShortName comments in bold with get and set accesors
==============================================================

   // Populate the list box using an array as DataSource.
            ArrayList USStates = new ArrayList();
            USStates.Add(new USState("Alabama", "AL"));
            USStates.Add(new USState("Washington", "WA"));
            USStates.Add(new USState("West Virginia", "WV"));
            USStates.Add(new USState("Wisconsin", "WI"));
            USStates.Add(new USState("Wyoming", "WY"));
            ListBox1.DataSource = USStates;

            // Set the long name as the property to be displayed and the short
            // name as the value to be returned when a row is selected.  Here
            // these are properties; if we were binding to a database table or
            // query these could be column names.
            ListBox1.DisplayMember = "LongName";
            ListBox1.ValueMember = "ShortName";

public class USState
    {
        private string myShortName;
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;
            this.myLongName = strLongName;
        }

        public string ShortName
        {
            get
            {
                return myShortName;
            }
        }

        public string LongName
        {

            get
            {
                return myLongName;
            }
        }

    }
}

解决方案

Getter are called when you read the value of the property:

USState state = new USState("Washington", "WA");

//...

string whereAmI = state.LongName; // calculation of the value calls getter


And setter is called when you assign a value to some property. You did not define any setters.

The whole idea of properties is the possibility to introduce some side effect behind the operations which as syntactically looks 100% identically to operations of reading or assignment of a field value.

[EDIT]

Please see also the comment by austinbox.

Your backing fields are redundant. In newer syntax, auto-implemented backing fields were introduced. This is all you need:

public class USState
    {

        public USState(string longName, string shortName)
        {
            this.ShortName = shortName;
            this.LongName = longName;
        }

        public string ShortName { get; } // backing field is implied 
        public string LongName { get; } // backing field is implied

    }
}



—SA


There are two parts in this code.

1) There are no set accessors in either of the two properties. The two variables are assigned values in the constructor.

2) These properties are then accessed from outside the class and these values are returned to the calling class during binding

ListBox1.DisplayMember = "LongName";
ListBox1.ValueMember = "ShortName";


这篇关于如何在c#中获取和设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆