What is internal set property in c#?

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

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.


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

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

Tags:

C#