Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve const-correctness in C#? [duplicate]

Possible Duplicate:
“const correctness” in C#

I have programmed C++ for many years but am fairly new to C#. While learning C# I found that the use of the const keyword is much more limited than in C++. AFAIK, there is, for example, no way to declare arguments to a function const. I feel uncomfortable with the idea that I may make inadvertent changes to my function arguments (which may be complex data structures) that I can only detect by testing.

How do you deal with this situation?

like image 258
andreas buykx Avatar asked Sep 25 '08 10:09

andreas buykx


People also ask

What is const correctness in C?

In C, C++, and D, all data types, including those defined by the user, can be declared const , and const-correctness dictates that all variables or objects should be declared as such unless they need to be modified.

Is const valid in C?

Yes. const is there in C, from C89.

Can you modify const in C?

In C or C++, we can use the constant variables. The constant variable values cannot be changed after its initialization.

Why do we use const in C?

We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value of the value of PI, you wouldn't like any part of the program to modify that value.


1 Answers

There is an excellent blog post about this issue by Stan Lippman: A question of const

like image 63
Armin Ronacher Avatar answered Nov 10 '22 06:11

Armin Ronacher