static readonly field initializer vs static constructor initialization

Posted by stackoverflowuser on Stack Overflow See other posts from Stack Overflow or by stackoverflowuser
Published on 2010-05-03T21:37:20Z Indexed on 2010/05/03 21:58 UTC
Read the original article Hit count: 316

Filed under:
|
|

Below are 2 different ways to initialize static readonly fields. Is there a difference between the 2 approaches? If yes, when should one be preferred over the other?

class A
{
   private static readonly string connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString;

}

class B
{
  private static readonly string connectionString;

  static B()
  {
     connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString;
  }  
}

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#3.0