473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What is const?

Atran
319 100+
Hello, What is const in C#? And do you can give me example.
Thanks.
May 31 '07 #1
6 1345
RedSon
5,000 Expert 4TB
The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable is constant, which means it cannot be modified. For example:

Expand|Select|Wrap|Line Numbers
  1. const int x = 0;
  2. public const double gravitationalConstant = 6.673e-11;
  3. private const string productName = "Visual C#";
May 31 '07 #2
zottty
27
Hello, What is const in C#? And do you can give me example.
Thanks.
readonly

Expand|Select|Wrap|Line Numbers
  1. public readonly int x;
May 31 '07 #3
Plater
7,872 Expert 4TB
ReadOnly
The same as in every other language that it appears (C,C++ and stuff)
May 31 '07 #4
SammyB
807 Expert 512MB
readonly

Expand|Select|Wrap|Line Numbers
  1. public readonly int x;
The difference between const and readonly is that Const's are really read only; and ReadOnly's are usually read only! In other words, Const's are just a convient way of writing a number in text form; for example,
const double PI = 3.14;
On the other hand, readonly fields are variables that are initialized in a constructor and cannot be changed during the life of the object.

See http://msdn2.microsoft.com/en-us/lib...b7(VS.71).aspx for more information. HTH --Sam
May 31 '07 #5
chinu
36
The difference between const and readonly is that Const's are really read only; and ReadOnly's are usually read only! In other words, Const's are just a convient way of writing a number in text form; for example,
const double PI = 3.14;
On the other hand, readonly fields are variables that are initialized in a constructor and cannot be changed during the life of the object.

See http://msdn2.microsoft.com/en-us/lib...b7(VS.71).aspx for more information. HTH --Sam
"const" is compile time where as "readonly" is runtime..
Jun 8 '07 #6
Atran
319 100+
Thanks for you all....
Jun 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Boogie El Aceitoso | last post by:
Hi, I have an exception class for winapi errors (returns a formated error mesage, usign ::GetLastError() et al, with what()). It looks like this: class WinapiException : public...
3
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
13
by: Peteroid | last post by:
Why does reading a member of a std::map not considered const? For example: class My_Class { int Get_Map_Value( int index ) const // ** error ** not considered const!!! { return m_Map ; //...
3
by: LuB | last post by:
I'm writing a Win32 application - and more specifically, doing event programming. I want the application to be const compliant but I'm faced with a bit of a conundrum. Physically, many of my...
19
by: pavan734 | last post by:
Hello, I dont know exact subject line for this question. In my class construcor I will read a value into a variable(member variable or a global variable). I need that variable to be constant...
14
by: Wolfgang | last post by:
As I understand, a const member function is used by const object to ensure that its instance isn't modified throughout its life. Am I missing something.. #include <iostream> using namespace...
2
by: EasyVoip | last post by:
Hello, I have a hard time understanding what is going on here. This looks like a normal function definition, but what const throw is doing on the same line? Thanks for your help!!!! const...
7
by: Luna Moon | last post by:
Hi all, I just couldn't get myself clear about the usage of "const" in front of and/or behind variables, pointers, classes, objects and functions... It's too confusing... any good clear...
3
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.