What is internal set property in c#?

Solution 1:

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.

Solution 2:

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; }
}

Solution 3:

Properties in C# 2.0

In C# 2.0 you can set the accessibility of get and set.

The code below shows how to create a private variable with an internal set and public get. The Hour property can now only be set from code in the same module (dll), but can be accessed by all code that uses the module (dll) that contains the class.

// private member variables
private int hour;

// create a property
public int Hour
{
  get { return hour; }
  internal set { hour = value; }
}

Solution 4:

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; }

Solution 5:

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;)