- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements