为什么我们需要在C#属性 [英] Why we need Properties in C#

查看:204
本文介绍了为什么我们需要在C#属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我什么是在C#中的属性我的意思是实际的解释的确切用法

在我们的项目中,我们使用的是性能,如

  ///<总结>
///列顺序
///< /总结>
保护INT m_order;

///<总结>
///获取/设置列顺序
///< /总结>
公众诠释订单
{
   {返回m_order; }
   集合{m_order =价值; }
}

///<总结>
///构造
///< /总结>
///<参数名称=名称>列名< /参数>
///< PARAM NAME =宽与GT;列宽和LT; /参数>
///< PARAM NAME =订单>列的顺序和LT; /参数>
公共viewColumn中(字符串名称,诠释的宽度,诠释顺序)
{
   //
   // TODO:添加构造函数逻辑这里
   //
   m_name =名称;
   m_width =宽度;
   m_order =秩序;
}


///<总结>
///返回列名,宽度和顺序列表视图。
///< /总结>
///<返回>字符串重的viewColumn中物体其中的present; /回报>
公共重写字符串的ToString()
{
  返程(的String.Format(列名= {0},宽度= {1},为了= {2}。,
        m_name,m_width,m_order));
}

///<总结>
///做2 viewColumn中的对象进行比较,看看它们是相同的。
///< /总结>
///< PARAM NAME =VC>作为比较&LT viewColumn中的对象; /参数>
///<返回>真如果对象是相同的,否则返回False< /回报>
公众覆盖布尔等于(obj对象)
{
   viewColumn中VC =(viewColumn中)目标文件;
   如果(m_name == vc.Name和放大器;&安培;
        m_width == vc.Width和放大器;&安培;
        m_order == vc.Order)
      返回true;
   其他
      返回false;
}
 

解决方案

想想看:你有一个房间,一进门就进入这个房间。如果您想查询如何谁是未来的,安全的房间,那么你应该以其他方式使用性质,他们不会受到任何的门,每一件来之不易的W / O型的任何规定。

 教室{
   公共字符串sectionOne;
   公共字符串sectionTwo;
}

房间R =新房间();
r.sectionOne =进入;
 

人们越来越到sectionOne pretty的轻松,有没有任何检查。

 类房
{
   私人字符串sectionOne;
   私人字符串sectionTwo;

   公共字符串SectionOne
   {
      得到
      {
        返回sectionOne;
      }
      组
      {
        sectionOne =检查(价值);
      }
   }
}

房间R =新房间();
r.SectionOne =进入;
 

现在你检查的人,了解他是否有邪恶的东西和他在一起。

Can you tell me what is the exact usage of properties in C# i mean practical explanation

in our project we are using properties like

/// <summary>
/// column order
/// </summary>
protected int m_order;

/// <summary>
/// Get/Set column order
/// </summary>
public int Order
{
   get { return m_order; }
   set { m_order = value; }
}

/// <summary>
/// constructor
/// </summary>
/// <param name="name">column name</param>
/// <param name="width">column width</param>
/// <param name="order">column order</param>
public ViewColumn(string name, int width, int order)
{
   //
   // TODO: Add constructor logic here
   //
   m_name = name;
   m_width = width;
   m_order = order;
}  


/// <summary>
/// returns the column name, width, and order in list view.
/// </summary>
/// <returns>string represent of the ViewColumn object</returns>
public override string ToString()
{
  return (string.Format("column name = {0}, width = {1}, order = {2}.", 
        m_name, m_width, m_order));
}

/// <summary>
/// Do a comparison of 2 ViewColumn object to see if they're identical.
/// </summary>
/// <param name="vc">ViewColumn object for comparison</param>
/// <returns>True if the objects are identical, False otherwise.</returns>
public override bool Equals(object obj)
{
   ViewColumn vc = (ViewColumn)obj;
   if(m_name == vc.Name &&
        m_width == vc.Width &&
        m_order == vc.Order)
      return true;
   else
      return false;
}

解决方案

Think about it : You have a room and a door to enter this room. If you want to check how who is coming in and secure your room, then you should use properties otherwise they won't be any door and every one easily come in w/o any regulation.

class Room {
   public string sectionOne;
   public string sectionTwo;
}

Room r = new Room();
r.sectionOne = "enter";

People is getting in to sectionOne pretty easily, there wasn't any checking.

class Room 
{
   private string sectionOne;
   private string sectionTwo;

   public string SectionOne 
   {
      get 
      {
        return sectionOne; 
      }
      set 
      { 
        sectionOne = Check(value); 
      }
   }
}

Room r = new Room();
r.SectionOne = "enter";

now you checked the person and know about whether he has something evil with him.

这篇关于为什么我们需要在C#属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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