
- 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
Final variables in C#
Java has a final keyword, but C# does not have its implementation. Use the sealed or readonly keyword in C# for the same implementation.
The readonly would allow the variables to be assigned a value only once. A field marked "read-only", can only be set once during the construction of an object. It cannot be changed.
Example
class Employee { readonly int age; Employee(int age) { this.age = age; } void ChangeAge() { //age = 27; // Compile error } }
Above, we have set the age field as readonly, which once assigned cannot be changed.
- Related Articles
- final variables in Java
- Final static variables in Java
- Final local variables in C#
- Why variables are declared final in Java
- What is blank final variable? What are Static blank final variables in Java?
- Difference between constants and final variables in Java?
- Assigning values to static final variables in java\n
- Can we declare final variables without initialization in java?
- Static and non static blank final variables in Java
- Effectively final variables in try-with-resources in Java 9?
- Interface variables are static and final by default in Java, Why?
- Can a method local inner class access the local final variables in Java?
- Explain final class and final method in PHP.
- final keyword in Java
- Final variable in Java

Advertisements