Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Is this field assignment safe?

In this snippet:

class ClassWithConstants
{
    private const string ConstantA = "Something";
    private const string ConstantB = ConstantA + "Else";

    ...

}

Is there a risk of ending up with ConstantB == "Else"? Or do the assigments happen linearly?

like image 422
Svish Avatar asked Aug 17 '09 13:08

Svish


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You will always get "SomethingElse". This is because ConstantB depends upon ConstantA.

You can even switch lines and you'll get the same result. The compiler knows that ConstantB depends upon ConstantA and will handle it accordingly, even if you write it in partial classes.

To be completely sure you can run VS Command Prompt and call ILDASM. There you can see the actual compiled code.

Additionally, if you try to do the following you'll get a compile error:

private const string ConstantB = ConstantA + "Else";
private const string ConstantA = "Something" + ConstantB;

Error: The evaluation of the constant value for 'ConsoleApplication2.Program.ConstantB' involves a circular definition This sort of proves the compiler knows its dependencies.


Added: Spec reference pointed out by Jon Skeet:

This is explicitly mentioned in section 10.4 of the C# 3 spec: Constants are permitted to depend on other constants within the same program as long as the dependencies are not of a circular nature. The compiler automatically arranges to evaluate the constant declarations in the appropriate order.


like image 188
Luis Filipe Avatar answered Sep 28 '22 16:09

Luis Filipe


this string concatenation happens at compile-time because there are only string literals (search for constant folding in compiler construction literature).

Don't worry about it.

like image 28
dfa Avatar answered Sep 28 '22 18:09

dfa