
- 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 local variables in C#
To set final for a local variable, use the read-only keyword in C#, since the implementation of the final keyword is not possible.
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.
Let us see an example. Below, we have set the empCount field as read-only, which once assigned cannot be changed.
Example
class Department { readonly int empCount; Employee(int empCount) { this. empCount = empCount; } void ChangeCount() { //empCount = 150; // Compile error } }
- Related Articles
- Can a method local inner class access the local final variables in Java?
- final local variable in Java
- final variables in Java
- Final variables in C#
- Local variables in Java
- Member variables vs Local variables in Java
- System variables vs Local Variables in MySQL?
- User-defined variables vs Local Variables in MySQL?
- Final static variables in Java
- What are class variables, instance variables and local variables in Java?
- What are local variables and global variables in C++?
- Global and Local Variables in Python?
- Global and Local Variables in Java
- Global vs Local variables in Python
- What are local variables in C++?

Advertisements