<p>I want to use CodeDOM to both declare and initialize my static field in one statement.  How can I do this?</p>

<pre><code>// for example
public static int MyField = 5;
</code></pre>

<p>I can seem to figure out how to declare a static field, and I can set its value later, but I can't seem to get the above effect.</p>

<p>@lomaxx,
Naw, I just want static.  I don't want const.  This value can change.  I just wanted the simplicity of declaring and init'ing in one fell swoop.  As if anything in the codedom world is simple.  Every type name is 20+ characters long and you end up building these huge expression trees.  Makes my eyes bug out.  I'm only alive today thanks to resharper's reformatting.</p>

[{'answer_id': 29534, 'author': 'Timothy Fries', 'author_id': 3163, 'author_profile': 'https://Stackoverflow.com/users/3163', 'pm_score': 4, 'selected': True, 'text': '<p>Once you create your CodeMemberField instance to represent the static field, you can assign the InitExpression property to the expression you want to use to populate the field.</p>\n'}, {'answer_id': 29538, 'author': 'Haacked', 'author_id': 598, 'author_profile': 'https://Stackoverflow.com/users/598', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://weblogs.asp.net/okloeten/archive/2005/10/15/427549.aspx" rel="nofollow noreferrer">This post</a> by Omer van Kloeten seems to do what you want. Notice that the output has the line:</p>\n\n<pre><code>private static Foo instance = new Foo();\n</code></pre>\n'}, {'answer_id': 29544, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 0, 'selected': False, 'text': '<p>I think what you want is a const rather than static. I assume what you want is the effect of having a static readonly which is why you always want the value to be 5.</p>\n\n<p>In c# consts are treated exactly the same as a readonly static.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/aa645749(VS.71).aspx" rel="nofollow noreferrer">From the c# docs</a>:</p>\n\n<blockquote>\n  <p>Even though constants are considered\n  static members, a constant-\n  declaration neither requires nor\n  allows a static modifier.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/29531', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/404/']