Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2398

Warning: implode() [function.implode]: Argument must be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3351

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3415

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2398

Warning: implode() [function.implode]: Argument must be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3351

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3415

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

Pages

Categories

Archives

C# and the readonly keyword


Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2026

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 2398

Warning: implode() [function.implode]: Argument must be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3351

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3374

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3415

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3467

Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

Warning: Invalid argument supplied for foreach() in /home/pvinson/scyanide.com/wp-content/plugins/wp-syntax/geshi/geshi.php on line 3612

I came across something interesting today and thought I’d share this small bit of information to the world. While this made me scratch my head for a second or two it really does make sense. In C#, and I assume VB.NET as well, we have the keyword readonly which allows one to initialize a read only variable during declaration or within a constructor. I recently had the situation where I needed to use a readonly but couldn’t figure out the best area to place it, either in my parent class or in each child class created. Being one who believes less code is better I leaned towards just throwing it in my parent class. It turns out though, that a read only variable can ONLY be initialized during its declaration or in the class the read only variable resides in and children classes WILL NOT be allowed to initialize the variable.

Here is a quick and dirty code example of what I’m talking about…

  public class ClassA
  {
    protected readonly String stringA;
 
    public ClassA() {}
  }
 
  public class ClassB : ClassA
  {
    public ClassB(String value)
    {
      stringA = value; // ERROR: A readonly field cannot be assigned to (except in a constructor or a variable initializer)
    }
  }

1 comment to C# and the readonly keyword

  • wiegraff13

    Looking it up in my CLR via C# book, readonly fields apparently can be modified via reflection; so you could technically set it in the inherited class, albeit in a roundabout manner.

Leave a Reply

Connect with Facebook

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">