0% found this document useful (0 votes)
32 views3 pages

Constants

Constants are values that cannot be changed at runtime. They must be integral types or enumerations and are initialized at declaration. Constants are implicitly static and can be accessed without creating an instance of the class they are declared in. Attempts to modify a constant's value or declare it as static will result in a compilation error, as constants are immutable and static modifiers are not needed.

Uploaded by

eackarthi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Constants

Constants are values that cannot be changed at runtime. They must be integral types or enumerations and are initialized at declaration. Constants are implicitly static and can be accessed without creating an instance of the class they are declared in. Attempts to modify a constant's value or declare it as static will result in a compilation error, as constants are immutable and static modifiers are not needed.

Uploaded by

eackarthi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

I.

Constants

- Constant members are the one whose value is remains same at all the
time.

- Declared using the keyword ‘const’, and initialized as they declared.

- Defined at compilation time and cannot be changed at runtime.

- Must be an integral types or enumeration.

- Constant members are implicitly static members, so they cannot use ‘static’
keyword explicitly to access. So, they can be accessed as same as static
variables in programs.

- Can be described with the accessibility modifiers public, private, protected.

Code Samples

(1). Declaration with initialization

using System;

public class consandreadonly


{

// Constant Member is declared and initialized here


const int HoursperDay = 24;

public consandreadonly()
{

(2). Declaration without initialization

public class consandreadonly


{
// Constant Member is declared with static modifer
const int HoursperDay;

# Note: This code will generate following compilation error

Error: A const field requires a value to be provided


(3). Assigning values to Constant member in run time by methods

public class consandreadonly


{
// Constant Member is declared and initialized here
const int HoursperDay = 24;

// Assigning value to constant member


public void assignvalue()
{
HoursperDay = 23;
}
}

# Note: This code will generate following compilation error

Error: The left-hand side of an assignment must be a variable, property or


indexer

(4). Assigning values to Constant member in run time by constructor

public class consandreadonly


{
// Constant Member is declared and initialized here
const int HoursperDay = 24;

// Assigning value to constant members in constructor


public consandreadonly()
{
//
// TODO: Add constructor logic here
//

HoursperDay = 23;
}

# Note: This code will generate following compilation error


Error: The left-hand side of an assignment must be a variable, property or
indexer

(5). Declaring Constant member with static keyword

public class consandreadonly


{
// Constant Member is declared with static modifer
static const int HoursperDay = 24;

# Note: This code will generate following compilation error , because constant
members are implicitly static members.

Error : The constant 'consandreadonly.HoursperDay' cannot be marked static

(6). Accessing Constant members outside the class in which they are
declared

public class consandreadonly


{
// Constant Member is declared with static modifer
public const int HoursperDay = 24;

public class consaccess


{

// Accessing constant member of 'consandreadonly' class without using


object
int Days = consandreadonly.HoursperDay;

You might also like