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# enum check in string value 
Csharp :: how to write a variable in console c# 
Csharp :: c# get pixel color from image 
Csharp :: c# return list 
Csharp :: rigidbody.addtorque 
Csharp :: c# repeat x times 
Csharp :: http error 502.5 asp.net core 2.2 
Csharp :: prettier inst working c# 
Csharp :: get time part from datetime as timestamp in c# 
Csharp :: how consider the first caracter in Split c# 
Csharp :: ggdesign 
Csharp :: bootstrap modal popup 
Csharp :: c# winforms textbox to int 
Csharp :: cs entity framework 
Csharp :: c# convert string to int 
Csharp :: c# array map 
Csharp :: c# linq remove duplicate items from list of integer 
Csharp :: decimal to string c# 
Csharp :: regular expression for email in c# 
Csharp :: c# todictionary linq 
Csharp :: how to loop over array in c# 
Csharp :: unity getcomponent not working on ui 
Csharp :: c# get enum value from string 
Csharp :: enum element count C# 
Csharp :: accessing form controls from another class c# 
Csharp :: c# remove duplicates from datatable 
Csharp :: copy 2d arrays C# 
Csharp :: c# ftp file download 
Csharp :: C# decimal with two places store as string with two places 
Csharp :: get client ip address c# 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =