Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

readonly vs const c#

readonly keyword is used to define a variable 
which can be assigned once after declaration 
either during declaration or in constructor. 
  
const keyword is used to define a constant to be used in the program.
Comment

c# const vs readonly

A const is a compile-time constant whereas readonly allows a value to be calculated at run-time and/but be set in the constructor or field initializer. So, a 'const' is always known at compile time but 'readonly' is not necessarily known at compiletime, but once it is assigned nant be changed.
constant and readonly both are not midificable. constant has to be known at compiletime while readonly could be left to be assign once in runtime limetedly(initilizer/constructor but not every wher of the code you like) 
with some modification to original
https://learn.microsoft.com/en-us/archive/blogs/ericlippert/immutability-in-c-part-one-kinds-of-immutability
https://stackoverflow.com/questions/55984/what-is-the-difference-between-const-and-readonly-in-c#:~:text=A%20const%20is%20a%20compile,on%20different%20types%20of%20immutability.
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# entity framework get all records from table 
Csharp :: sum the digits in c# 
Csharp :: unity agent look at 
Csharp :: c# inheritance 
Csharp :: get quaternion from vector unity 
Csharp :: color32 unity 
Csharp :: add list to list c# 
Csharp :: c# method returns multiple values 
Csharp :: unity reflect raycast 
Csharp :: unity unit tests 
Csharp :: c# get a value from value tuple list 
Csharp :: timer unity 
Csharp :: print c# 
Csharp :: edit list element linq c# 
Csharp :: rotation 
Csharp :: c# xmldocument from file 
Csharp :: How to make a simple console select screen using C# ReadKey 
Csharp :: c# calendar button random dates 
Csharp :: unity dropdown 
Csharp :: clickable table row asp.net core 
Csharp :: c# filter datagridview 
Csharp :: verifyusertokenasync password reset token 
Csharp :: devexpress spreadsheet document source wpf 
Csharp :: unity how to find the largest value out of 2 numbers 
Csharp :: oracle c# parameters wont work 
Csharp :: c# get file author 
Csharp :: c# how to get a file path from user 
Csharp :: Response.Redirect cannot be called in a Page callback 
Csharp :: double quotes in a string c# 
Csharp :: how to compare time strings in c# 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =