Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Constants

A constant is a symbol that has a never-changing value. When defining a constant symbol, its value must be determinable at compile time. The compiler then saves the constant’s value in the assem- bly’s metadata. This means that you can define a constant only for types that your compiler considers primitive types. In C#, the following types are primitives and can be used to define constants: Boolean, Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, and String. However, C# also allows you to define a constant variable of a non-primitive type if you set the value to null.

 

using System;

 

public sealed class SomeType {

// SomeType is not a primitive type but C# does allow

// a constant variable of this type to be set to 'null'. public const SomeType Empty = null;

}

 

Because a constant value never changes, constants are always considered to be part of the defin- ing type. In other words, constants are always considered to be static members, not instance mem- bers. Defining a constant causes the creation of metadata.

When code refers to a constant symbol, compilers look up the symbol in the metadata of the as- sembly that defines the constant, extract the constant’s value, and embed the value in the emitted Intermediate Language (IL) code. Because a constant’s value is embedded directly in code, constants don’t require any memory to be allocated for them at run time. In addition, you can’t get the address of a constant and you can’t pass a constant by reference. These constraints also mean that constants don’t have a good cross-assembly versioning story, so you should use them only when you know that


the value of a symbol will never change. (Defining MaxInt16 as 32767 is a good example.) Let me demonstrate exactly what I mean. First, take the following code and compile it into a DLL assembly.

 

using System;

 

public sealed class SomeLibraryType {

// NOTE: C# doesn't allow you to specify static for constants

// because constants are always implicitly static. public const Int32 MaxEntriesInList = 50;

}

 

Then use the following code to build an application assembly.

 

using System;

 

public sealed class Program { public static void Main() {

Console.WriteLine("Max entries supported in list: "

+ SomeLibraryType.MaxEntriesInList);

}

}

 

You’ll notice that this application code references the MaxEntriesInList constant defined

in the SomeLibraryType class. When the compiler builds the application code, it sees that Max­ EntriesInList is a constant literal with a value of 50 and embeds the Int32 value of 50 right inside the application’s IL code, as you can see in the following IL code. In fact, after building the application assembly, the DLL assembly isn’t even loaded at run time and can be deleted from the disk because the compiler does not even add a reference to the DLL assembly in the application's metadata.



 

.method public hidebysig static void Main() cil managed

{

.entrypoint

// Code size 25 (0x19)

 

.maxstack 8

IL_0000: nop  
IL_0001: ldstr "Max entries supported in list: "
IL_0006: ldc.i4.s
IL_0008: box [mscorlib]System.Int32
IL_000d: call string [mscorlib]System.String::Concat(object, object)
IL_0012: call void [mscorlib]System.Console::WriteLine(string)
IL_0017: nop  
IL_0018: ret  

} // end of method Program::Main

 

This example should make the versioning problem obvious to you. If the developer changes the MaxEntriesInList constant to 1000 and only rebuilds the DLL assembly, the application assembly is not affected. For the application to pick up the new value, it will have to be recompiled as well. You can’t use constants if you need to have a value in one assembly picked up by another assembly at run time (instead of compile time). Instead, you can use readonly fields, which I’ll discuss next.



Date: 2016-03-03; view: 658


<== previous page | next page ==>
Nbsp;   Components, Polymorphism, and Versioning | Nbsp;   Fields
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)