关于获取和设置的快速问题 [英] Quick Question About Get and Set

查看:107
本文介绍了关于获取和设置的快速问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在类中存在以下代码,那么与变量关联的get和set方法是什么?

  public string Something {get;}如何获取并设置类的实例?组; } 


解决方案

-



  private string something; 
public string Something
{
get {return something; }
set {something = value;为了访问这个变量(应该是在MyClass类中):{
}

  // GET 
MyClass myObj = new MyClass();
string test = myObj.Something;
// SET
myObj.Something =blabla;


If there is the following code in a class, are get and set methods associated to the variable? How can I access get and set with an instance of the class?

public string Something { get; set; }

解决方案

This syntax comes with .Net Framework 3.5 (automatic-property)

It's like :

private string something;
public string Something
{
     get { return something; }
     set { something = value; }
}

To access to this variable (supposed to be in a MyClass class) :

// GET
    MyClass myObj = new MyClass();
    string test = myObj.Something;
// SET
    myObj.Something = "blabla";

这篇关于关于获取和设置的快速问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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