C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
Java
Learn iOS Programming
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
1
Reply
Difference between constant and readonly c# ?
Rohan Gupta
10y
1.1k
0
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
Constants:Constants are declared using a "const" keyword.Constants should be assigned a value at the time of the variable declaration and hence are known at compile time.example :void add(int Z) {const int X = 10, X1 = 50;const int Y = X + X1; //no error, since its evaluated a compile timeconst int Y1 = X + Z; //gives error, since its evaluated at run time }Readonly variables are known at runtime, they can be assigned a value either at runtime or at the time of the instance initialization or with in the constructor of same class. class MyClass {readonly int X = 10; // initialized at the time of declarationreadonly int X1;public MyClass(int x1){X1 = x1; // initialized at run time} }
Rohan Gupta
10y
0
Message