为什么在C#Bean文件中使用私有字符串 [英] Why use private string in c# Bean File

查看:122
本文介绍了为什么在C#Bean文件中使用私有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,
在C#项目的Bean文件中,我们定义set和get方法.
但是在定义此名称之前,我们先将名称声明为私有字符串.为什么还要将名称声明为私有?是出于安全原因还是其他原因?

Hello Sir,
In the Bean File of C# project we define the set and get methods.
But before define this we declare names as a private string.. So why we declare the name as private? Is this for security reason or any other?

推荐答案

C#中的Bean文件?

关于private访问修饰符:
私有:类型或成员只能由相同类或结构中的代码访问.

在此处阅读您想了解的所有访问修饰符的所有信息:
MSDN:访问修饰符(C#编程指南) [
Bean file in C#?

About private access modifier:
private: The type or member can be accessed only by code in the same class or struct.

Read all you want to know about all access modifiers here:
MSDN: Access Modifiers (C# Programming Guide)[^]


这不是出于安全性考虑.访问修饰符不能保护成员免受访问.借助反射,任何人都可以访问任何成员,包括私有成员和内部成员.这样做的目的与编程语言和系统的所有其他功能相同,目的是支持将编程作为人类活动来进行,从而减少编程的繁琐度和出错率.访问修饰符可保护开发人员免受一切可访问和允许的错误的侵害.

-SA
This is not for security. Access modifier does not protect a member from access. With Reflection, anyone can access any members, including private and internal. This is done for the same purpose as all other features of programming languages and systems — to support programming as a human activity, to make programming less tedious and error prone. Access modifiers protect developers from additional mistakes usual when everything is accessible and allowed.

—SA


属性是一种成员,它提供了一种灵活的机制来读取,写入或计算私有字段的值.可以将属性当作公共数据成员使用,但实际上它们是称为访问器的特殊方法.这样可以轻松访问数据,并且仍然有助于提高方法的安全性和灵活性.

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.


因此,如果私有变量不是私有变量,则其他类可以获取/设置该变量的值.然后,私有变量将跳过该属性的获取/设置过程.


So, if the private variable isn''t private, then other classes can get/set the value of that variable. And then the private variable skips the get/set process from the property.


这篇关于为什么在C#Bean文件中使用私有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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