Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Loops and Final Touches Magic Numbers and Constants

Duc Duong
Duc Duong
8,670 Points

"readonly" and "const" type??

I wonder if these 2 types "readonly" and "const" have any differences?

1 Answer

Steven Parker
Steven Parker
230,970 Points

A "const" can only be assigned by initialization during the compile. A "readonly" can be assigned in a constructor at run-time.

Jun Dong
Jun Dong
4,754 Points

So it's okay to switch out the const keyword and replace it with readonly and it'll work the same?

Steven Parker
Steven Parker
230,970 Points

My point was that they are different. For example, you couldn't use "const" for something that was going to get set to a value that is unknown until an object instance is being created as the program runs.