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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
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.
Suppose you're designing an API for use by other programmers. Within this API, you have an object
Foo
which has a propertyBar
. You don't want the other programmers setting the value ofBar
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:If a property setter is marked with the internal access modifier, only classes that reside within the assembly can set the property.
given a property declared
public string MyString {get; internal set;}
this means thatpublic string MyString
)internal set;
)It means that the property can only be set by code that resides within the same assembly as the class delcaring the property.
It is a construct which allows the value of a property to be set only by code within the same assembly.