
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are the benefits to marking a field as readonly in C#?
The readonly keyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of.
The 'readonly' modifier can be used in a total of four contexts:
Field declaration
Readonly struct declaration
Readonly member definition
Ref read only method return
When we use the field declaration context, we need to know that the only time the assignment can occur is when it is declared or when the constructor of the same class is invoked.
Example
class Program{ readonly string Name; public Program(){ Name = "Name"; } static void Main(string[] args){ Program a = new Program(); System.Console.WriteLine(a.Name); Console.ReadLine(); } }
Output
Name
readOnly Struct
public readonly struct Server{ public readonly string Name; public Server(string name){ Name = name; } } class Program{ static void Main(string[] args){ Server a = new Server("Domain Controller"); System.Console.WriteLine(a.Name); Console.ReadLine(); } }
Output
Domain Controller
- Related Articles
- What are the benefits of using Selenium as an Automation Testing tool?
- What is the difference between keywords const and readonly in C#?
- Marking Methods in Textiles
- What are the benefits of listening to music?
- What are the benefits of a module in Java 9?
- What are the benefits of wheatgrass?
- What are the benefits of meditation?
- What Are the Benefits of Bioprocessing?
- What Are the Benefits of a Digital Marketing Agency?
- Const vs Static vs Readonly in C#
- What MySQL returns if the list of strings, provided as argument in FIELD() function, are NULL?
- What are the benefits of using MySQL views as compared to selecting data directly from MySQL base tables?
- What are the benefits of Networking in information security?
- What are the health benefits of Clove?
- What are the health benefits of spinach?

Advertisements