-
Notifications
You must be signed in to change notification settings - Fork 1
Properties
Mario Gutierrez edited this page Jan 7, 2017
·
1 revision
Properties are an implementation of the accessor/mutator (getter/setter) pattern.
A basic .NET property:
public int SomeInt
{
get { return myInt; }
set { myInt = value; }
}
public int SomeInt { get; set; }
This uses a private field internally and sets that to a default value.
For ReferenceTypes, the default value is null; which can be a problem. In many cases, the default value will not be what you want. The solution to this is to assign a default value in the constructor. Alternatively, C# 6.0 includes a special syntax.
public string SomeString { get; set; } = "Default value.";
- Abstract Classes
- Access Modifiers
- Anonymous Methods
- Anonymous Types
- Arrays
- Attributes
- Console I/O
- Constructors
- Const Fields
- Delegates
- Enums
- Exceptions
- Extension Methods
- File IO
- Generics
- Interfaces
- Iterators
- LINQ
- Main
- Null Operators
- Parameters
- Polymorphism
- Virtual Functions
- Reflection
- Serialization
- Strings
- Value Types
- "Base" Keyword
- "Is" and "As"
- "Sealed" Keyword
- nameof expression