属性和字段之间的区别.. [英] Difference between Properties and Fields..

查看:110
本文介绍了属性和字段之间的区别..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

谁能解释一下csharp中属性"和普通字段"之间的区别是什么?如果您显示一些示例,那就更好了...

我在将null分配给整数属性时遇到了一些错误....

Hai,

Can anybody explain me whats the difference between Properties and normal Fields in csharp. If u show some example then it would be more better...

And i got some error here while assigning null to a integer property....

private int a;
public int A
{
    get {
        return a;
    }
    set {
        a = value;
    }
}
private int b;
public int B
{
    get { return b; }
    set { b = value; }
}


分配
*********


Assigning
*********

Class1 c1 = new Class1

c1.A= int.parse(tb1.text)  // Here text box is empty so getting error

c1.B=int.parse(tb2.text)   


有人可以向我解释一下吗.....

在此先感谢...


Can anybody explain me regarding this.....

Thanks in advance...

推荐答案

要了解属性与字段的不同,请想象您已经从Label控件派生了一个类,并将其添加更改显示文本的前景色的功能.您的代码将类似于以下内容:

To understand how a property is different from a field, imagine you have derived a class from a Label control, to add it the capability to change the foreground color of the shown text. Your code will be similar to the following:

public class ColoredLabel : Label
{
   private Color _Foreground = Color.Black;

   ...

   public Color Foreground
   {
      get
      {
         return _Foreground;
      }
      set
      {
         _Foreground = value;
         Invalidate();
      }
   }

   ...

}



如您所见,使用属性不仅可以简单地获取和/或设置字段的值,还可以做更多的事情.

关于您遇到的错误,请参见 Int32.Parse方法的文档(字符串) [ ^ ]:由于在某些情况下可能会失败并引发异常,因此您必须在代码中添加异常处理以使其健壮:



As you can see, using a property allow you to do more than simply get and/or set the value of a field.

About the error that you are getting, see the documentation of the Int32.Parse Method (String)[^]: as it could fails and throws an exception in some cases, you have to add exception-handling to your code to make it robust:

try
{
   c1.A = int.Parse(tb1.Text);
}
catch (FormatException e)
{
   // The text in the TextBox doesn't match an int
   ...
}
catch (OverflowException e)
{
   // The number in the TextBox is out of the admitted range for an int
   string text = tb1.Text;
   text.Trim();
   if (text.StartsWith("-"))
      c1.A = int.MinValue;
   else
      c1.A = int.MaxValue;
}
catch (Exception e)
{
   // Something else goes wrong
   ...
}


在上面的示例中,private int a;是一个字段,而public int A是一个属性;该属性包含一个setter和/或getter,并且还可以包括验证或其他支持代码.字段仅仅是变量.

在您的赋值语句中,您会收到错误消息,因为null不是整数,因此无法赋给c1.A. .NET的更高版本引入了可为空的变量的概念.
In your sample above private int a; is a field while public int A is a property; the property contains a setter and/or getter, and may also include validation or other supporting code. A field is merely a variable.

In your assignment statement you receive an error because null is not an integer and so cannot be assigned to c1.A. Later versions of .NET introduce the concept of nullable variables.


使用int.TryParse方法.它返回一个bool,指示它是否是一个有效的整数,这样您就不必依靠try/catch机制了(当您可以轻松地进行验证/验证时,抛出异常确实没有意义.对预期结果做出反应.
Use the int.TryParse method. It returns a bool that indicates if it was a valid integer so that you don''t have to rely on the try/catch mechanism (there''s really no point in throwing an exception when you can otherwise easily enough verify/react to the expected results.


这篇关于属性和字段之间的区别..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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