在ASP.NET C#中使用Accessor [英] Using Accessors in ASP.NET C#

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

问题描述





请帮我学习C#中的Accessors。不知道。请帮忙...





谢谢

Hi,

Kindly help me to learn Accessors in C#. Have got no idea. Please help...


Thanks

推荐答案

访问者基本上可以帮助你得到并设置私有变量的值,因为你不想将这个变量暴露给外界。



声明一个私有变量:



Accessors basically assist you to get and set the value of a private variable since you don't want to expose that very variable to the outside world.

Declare a private variable:

public class Emp{
private string name;
}





使用属性分配和检索其值:





Use a property to assign and retrieve its values:

public string Name{

get{return name;}
set{name=value;}
}





value是一个隐式参数,用于设置私有变量的值。



现在,让我们看看我们如何使用访问器:





value is an implicit parameter used to set the value of the private variable.

Now, lets see how can we use the accessors:

static void Main()
{
Emp obj=new Emp();
obj.Name="Honey";//this line of code will call the set accessor, here value=Honey
Console.WriteLine(obj.Name);//this line of code will call the get accessor and will print   the name Honey
}





如需进一步了解,请参阅以下链接:

http://msdn.microsoft.com/en-us/library/aa287786(v = vs.71)的.aspx [ ^ ]



-Anurag



For further understanding, refer to the below link:
http://msdn.microsoft.com/en-us/library/aa287786(v=vs.71).aspx[^]

-Anurag


MSDN 学习:

访问者 [ ^ ]

限制访问者辅助功能 [ ^ ]
Learn from MSDN:
Accessors[^]
Restricting Accessor Accessibility[^]


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

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