What is internal set property in c#?

2020-05-20 05:53发布

I just came across an unknown concept of c# for me. Can anyone tell me what the purpose of an internal set property is? What is its use? I know internal keyword is used for working within an assembly.

标签: c#
7条回答
萌系小妹纸
2楼-- · 2020-05-20 06:23

If you have a property with an internal set accessor (and public get accessor) it means that code within the assembly can read (get) and write (set) the property, but other code can only read it.

You can derive the above information by reading about the internal access modifier, the public access modifier and properties.

Also, you can read about Restricting Accessor Accessibility.

查看更多
爷、活的狠高调
3楼-- · 2020-05-20 06:25

Suppose you're designing an API for use by other programmers. Within this API, you have an object Foo which has a property Bar. You don't want the other programmers setting the value of Bar when they reference your assembly, but you have a need to set the value yourself from within your API. Simply declare the property as such:

public class Foo
{
   public string Bar { get; internal set; }
}
查看更多
家丑人穷心不美
4楼-- · 2020-05-20 06:27

If a property setter is marked with the internal access modifier, only classes that reside within the assembly can set the property.

public string MyProperty { get; internal set; }
查看更多
放我归山
5楼-- · 2020-05-20 06:33

given a property declared public string MyString {get; internal set;} this means that

  • you can read the property's value from anywhere withhin your application (public string MyString)
  • but you may only write a new value to the property from inside the assembly in which it is declared - or from friend assemblies. (internal set;)
查看更多
贪生不怕死
6楼-- · 2020-05-20 06:34

It means that the property can only be set by code that resides within the same assembly as the class delcaring the property.

查看更多
我只想做你的唯一
7楼-- · 2020-05-20 06:37

It is a construct which allows the value of a property to be set only by code within the same assembly.

查看更多
登录 后发表回答