Home > Backend Development > C#.Net Tutorial > Final variables in C#

Final variables in C#

WBOY
Release: 2023-09-06 22:41:12
forward
785 people have browsed it

C# 中的最终变量

Java has the final keyword, but C# does not have its implementation. The same implementation is achieved in C# using seal or readonly keyword.

readonly allows a variable to be assigned a value only once. Fields marked "read-only" can only be set once during object construction. It cannot be changed.

Example

class Employee {
   readonly int age;

   Employee(int age) {
      this.age = age;
   }

   void ChangeAge() {
         //age = 27; // Compile error
   }
}
Copy after login

Above, we set the age field to read-only and cannot be changed once assigned.

The above is the detailed content of Final variables in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template