Getter和Setter在.net中使用 [英] Getter and Setters use in .net

查看:74
本文介绍了Getter和Setter在.net中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述











有什么区别

1)





Private int myInt;



public int MyInt

{

get {return _myInt.Value; }

set {_myInt.Value = value; }

}



2)



public int MyInt;





这是什么用途



public int MyInt
{

get {return _myInt.Value; }

set {_myInt.Value = value; }

}



任何人都可以帮我一个例子。

解决方案

< blockquote>首先,您的代码示例都不会编译。让我们尝试按照编译的方式重新表达问题:

  class  SomeDeclaringType {

private int myInt; // private可以省略; 默认
//会发生什么如果您将其更改为 public

public int MyInt {//可能 internal 私有
获取 { return _myInt.Value; }
set {_ myInt.Value = value; }
}

// ...

}



首先,你可以很容易在这里找到所有答案: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx [

title =新窗口 > ^
]。

了解访问者的使用;我不想重复文档。



您可以将代码与更改 private 到 public (或 internal ;顺便说一下,阅读有关访问修饰符的信息:http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx [ ^ ]。)有什么区别?好吧,它会以同样的方式工作,只有代码完全没有意义:你会向用户公开 myInt ,它将提供与完全相同的功能敏。这不是很明显吗?



此外,你根本不需要 myInt ,因为那些琐碎的getter和setter可以自动实现: http://msdn.microsoft.com/en-us/ library / bb384054.aspx [ ^ ]。



因此,此代码与第一个代码示例完全相同:

  class  SomeDeclaringType {
public int MyInt { get ; set ; }
// ...
}



请注意,此属性提供的功能与公共字段完全相同。为什么我们使用那些琐碎的属性呢?嗯,这主要是文化的元素,也是代码的可维护性。首先,使用任何公共(甚至内部)字段被认为是不好的风格;并且使用属性被认为是好的风格。再次,为什么? 因为公共/内部成员是合同的一部分。该属性允许在不改变合同的情况下添加功能。你可以通过在setter和getter中添加一个body来实现;只有当您需要在读取属性值或将值赋值给属性时添加副作用时才需要此正文。



-SA


好吧,有一个共同的因素:它们都不会编译......



真的吗?

1) int 没有Value属性。所以要么你的属性设置器和getter都错了,要么_myInt命名错误......

2)第二个版本试图声明两个具有相同名称的不同对象: MyInt 不能是同一范围内的变量和属性。



我认为你需要回到你的作业问题,看一点更接近它所问的问题:因为它不是那样的!


Hi ,



What is the difference between

1)


Private int myInt;

public int MyInt
{
get { return _myInt.Value; }
set { _myInt.Value = value; }
}

2)

public int MyInt;


what is the use of this

public int MyInt
{
get { return _myInt.Value; }
set { _myInt.Value = value; }
}

could any one help me with a example.

解决方案

First of all, none of your code sample will compile. Let's try to reformulate the question the way it compiles:

class SomeDeclaringType {
    
    private int myInt; // "private" could be omitted; this is the default
    // what happens if you change it to "public"?

    public int MyInt { // could be "internal" instead of "private"
        get { return _myInt.Value; }
        set { _myInt.Value = value; }
    }
    
    //...

}


First of all, you could easily find all answers here: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx[
title="New Window">^
].
Read about the use of accessors; I don't want to repeat the documentation.

You could compare the code with the case where you change private to public (or internal; by the way, read about access modifiers: http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx[^].) What's the difference? Well, it would work the same way, only the code would be totally pointless: you would expose myInt to the user which would provide exact same functionality as MyInt. Isn't it obvious?

Moreover, you don't need to have myInt at all, because those trivial getter and setter can be auto-implemented: http://msdn.microsoft.com/en-us/library/bb384054.aspx[^].

So, this code would be 100% identical to the first code sample:

class SomeDeclaringType {
    public int MyInt { get; set; }
    //...
}


Note that this property provides same exact functionality as just the public field. Why we use those trivial properties then? Well, this is mostly the element of culture, but also the maintainability of the code. First of all, using any public (or even internal) fields is considered bad style; and using properties is considered good style. Again, why? Because the public/internal member is a part of contract. And the property allows to add functionality without changing contract. You can do it by adding a body to the setter and getter; and this body is only needed when you need to add a side effect to the operation of reading of the property value or assigning the value to the property.

—SA


Well, ther share a common factor: neither of them will compile...

Really?
1) int does not have a Value property. So either your property setters and getters are wrong, or _myInt is badly named...
2) The second version tries to declare two different objects with the same name: MyInt cannot be a variable and a property in the same scope.

I think you need to go back to your homework question, and look a little closer at exactly what it is asking you: because it isn't that!


这篇关于Getter和Setter在.net中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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