属性和公共变量之间的区别(Asp.net(C#)) [英] Difference between Properties and Public Variables(Asp.net(C#) )

查看:68
本文介绍了属性和公共变量之间的区别(Asp.net(C#))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我只是想知道公共变量和类的公共属性之间的区别。

解决方案

具有getter和setter的属性,将来可以更改而不影响所有其他代码库。但你不能用变量做同样的事情。

你也可以通过使用属性来制作只读等但不能用公共变量做


In C#,默认情况下,类中定义的所有属性都是public.Properties获取和设置值。 C#语言提供了它们作为简化语法的便捷方式。它们被实现为中间语言中的方法。它们是来自外部代码的类的标准访问点。

例如:

  public   class  clsMyClass 
{
public 字符串 myproperty1 {获取; set ;}

}

是C#中可用的自动属性类型的示例。



现在'问题'产生了在公共变量中使用C#中的属性的主要好处。



*公共变量是不安全,因为每个人都可以设置任何价值,而你无法控制它们,而在财产的情况下,你可以检查没有人可以设置无效价值。



*使用属性我们可以限制任何一个,这意味着他/她只能获取值或读取值但不能从类外设置值,反之亦然。



*您无法使用公共变量进行调试,但您可以使用属性进行调试。



*变量/字段是一个实现并通过公开直接你是'打破封装'。对象现在无法知道它的状态已经改变了,这是一件坏事。



*'数据绑定根本不承认字段,它只适用于属性。



*属性不必引用'实际变量'。

例如:

  public   class  clsExample 
{
private int _num1;
private int _num2;
public int Sum
{获取 { return _num1 + _num2};}
}



*属性可以是接口成员,而变量不能是成员。



*变量可以通过引用传递,而属性不能。



*变量只有一个访问修饰符,而属性在get和set上有不同的修饰符。


没有主要和不是主要的差异,整个差异的概念毫无意义。这只是浪费时间和错过事物本质的方式。



属性是提供字段语法的方式,即读取和分配值,同时提供一种机制来定义这些操作背后的副作用,它们中的一个,或者其中之一。在后一种情况下,您可以在不更改类型界面的情况下添加此类效果。



其他所有内容都是这样做的结果。你试图把所有东西扔进一堆,这表明缺乏理解和错误的思维方式。



-SA

Hi Experts,

I just want to know the difference between public variable and public property of a class.

解决方案

properties having getters and setters which can change in future without effecting to all your other code base. But you can't do the same with variable.
also you can make read only etc by using properties but can't do it with public variable


In C#, by default all the properties defined in a class are always public.Properties get and set values. The C# language provides them as a convenient way to simplify syntax. They are implemented as methods in the intermediate language. They are standard access points to a class from external code.
Eg:

public class clsMyClass
{
public string myproperty1{get; set;}

}

is an example of a automatic property type available in C#.

Now the 'Question' arises what are major benefits of using Properties in C# over Public variables.

* Public variables are unsafe as everyone can set any value and you have no control over them, while in case of property you can put a check that no one could set invalid value to them.

* Using Properties we can restrict any one, that means he/she can only get the value or read the value but could not set the value from outside of class and vice- a- versa.

* You can not debug with public variables but you can do that with property.

* A variable/field is an implementation and by exposing it directly you are 'breaking encapsulation'.The object now has no way to know its state has changed and that's a bad thing.

* 'Databinding' doesn't acknowledge fields at all, it will only work with properties.

* Properties do not have to refer to an 'actual variable'.
Eg:

 public class clsExample
{
private int _num1;
private int _num2;
public int Sum
{get{return _num1+_num2};}
}


* A property can be a interface member while a variable can't be a member.

* A variable can be passed by reference while a property can't be.

* A variable has only one access modifier while a property have different modifiers on get and set.


There are no "major" and not major "differences", and the whole concept of "difference" makes no sense. It's just a waste of time and the way to miss the essence of things.

Property is the way to provide the syntax of the field, that is, reading and assigning value, while providing a mechanism to define a side effect behind these operations, both of them, one of them, or none of then. In latter case, you get the way to add such effect without changing the type interface.

Everything else is just the consequence of this. You are trying to throw everything in one pile, which is a sign of lack of understanding and wrong way of thinking.

—SA


这篇关于属性和公共变量之间的区别(Asp.net(C#))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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