Thursday, February 4, 2010

C# - Change compilation debug value of the configuration file at runtime

Below is a C# code snippet to demonstrate on how to change the compilation-debug value at runtime.
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");
            
CompilationSection compSection = ((CompilationSection)(config.GetSection("system.web/compilation")));
            
compSection.Debug = true;

config.Save();

Please note that there might be times that this section is locked, so it is always helpful to check them first. Below is a revision C# code snippet to show that.
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");
CompilationSection compSection = ((CompilationSection)(config.GetSection("system.web/compilation")));
            
compSection.Debug = true;
            
if (!(compileSection.SectionInformation.IsLocked))
{
     config.Save();
}

Tuesday, February 2, 2010

C# - Manipulating the appSettings section of the configuration file at runtime

There might be a time wherein you have to manipulate the configuration at runtime, either adding or removing key-value pair. Below are some code snippets in C# to do this.

Adding a new appSetting value:
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");

AppSettingsSection appSection = ((AppSettingsSection)(config.GetSection("appSettings")));

KeyValueConfigurationCollection keyVal = appSection.Settings;

keyVal.Add("myNewValue", "value" + ++intCounter);

config.Save();

Removing the value:
Configuration config = WebConfigurationManager.OpenWebConfiguration("/WebPersonalExercises");

AppSettingsSection appSection = ((AppSettingsSection)(config.GetSection("appSettings")));

KeyValueConfigurationCollection keyVal = appSection.Settings;

keyVal.Remove("myNewValue");

config.Save();

Note that if you add the same appSetting key multiple times, it would be presented in the file like this (concatenated with a comma separator):
<appSettings>
  <add key="myNewValue" value="value1,value2,value3"/>
</appSettings>

But when you call the remove method, all of the appSetting key-value would be removed, regardless of how many times you add the same appSetting key of different values.

Wednesday, January 27, 2010

Data Type Range: Floating-Point and Decimal Data Types

Another thing that confused me is the Floating-Point and Decimal Data Types. So this is again my reminder.

  • float - based on System.Single
    • 32-bit single-precision floating point number between -3.402832 x 1038 and -1.401298 / 1045 (for negative values) and between 1.401298 / 1045 and 3.402832 x 1038 (for positive values)
    • numeric literal - F or f. e.g. float price = 345.45F;
  • double - based on System.Double
    • 64-bit double-precision floating point number between -1.79769313486231570 x 10308 and -4.94065645841246544 / 10324 (for negative values) and between 4.94065645841246544 x 10324 and 1.79769313486231570 x 10308
    • numeric literal - D or d. e.g. double salary = 2500.50D;
  • decimal - based on System.Decimal
    • 128-bit number between -79,228,162,514,264,337,593,543,950,335 and 79,228,162,514,264,337,593,543,950,335 with no decimal places and between -7.9228162514264337593543950335 and 7.9228162514264337593543950335 with up to 28 decimal places
    • holds numbers of lesser range than floating points but with greater precision
    • numeric literal - M or m. e.g. decimal width = 3.68M;

More of c# literal could be found here.

Tuesday, January 26, 2010

Data Type Range: Integer

I've been developing C# for so many years. As I work through my certification (a goal for a very long time :D) I always miss the very basic things about C#. For example, I could not really differentiate the Integer data types with their range and size. So this post serves a my own reminder for that.

  • sbyte - based on System.SByte
    • 8-bit signed integer between -128 and 127
  • byte - based on System.Byte
    • 8-bit integer between 0 and 255
  • short - based on System.Int16
    • 16-bit integer between -32,768 and 32,767
  • ushort - based on System.Int16
    • 16-bit unsigned integer between 0 and 65,535
  • int - based on System.Int32
    • 32-bit integer between -2,147,483,648 and 2,147,483,647
  • uint - based on System.Int32
    • 32-bit unsigned integer between 0 and 4,294,967,295
  • long - based on System.Int64
    • 64-bit integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
  • ulong - based on System.Int64
    • 64-bit unsigned integer between 0 and 18,446,744,073,709,551,615

We have to take note on the memory requirement and performance upon using these data type as we code. int requires twice as much storage of space (4 bytes) than short (2 bytes).

But as a recommendation we have to use int or long because they are more efficient than byte and short because .Net represents numbers as a 32-bit or a 64-bit values. Unless you have a valid concern about memory usage, then you could use the specific data type for your needs.