<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C# Frequently Asked Questions : General</title><link>http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx</link><description>Tags: General</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How do I create a constant that is an array?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/12/03/274417.aspx</link><pubDate>Fri, 03 Dec 2004 20:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:274417</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/274417.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=274417</wfw:commentRss><description>&lt;p&gt;
Strictly speaking you can't, since const can only be applied to a field or local whose value is known at compile time.
&lt;/p&gt;&lt;p&gt;
In both the lines below, the right-hand is not a constant expression (not in C#).
&lt;/p&gt;
&lt;pre
&gt;const int [] constIntArray = newint [] {2, 3, 4};
 // error CS0133: The expression being assigned to 'constIntArray' must be constant
const int [] constIntArrayAnother = {2, 3, 4};
 // error CS0623: Array initializers can only be used in a variable or field
 //               initializer. Try using a new expression instead. 
&lt;/pre&gt;
&lt;p&gt;
However, there are some workarounds, depending on what it is you want to achieve.
&lt;/p&gt;&lt;p&gt;
If want a proper .NET array (System.Array) that cannot be reassigned, then static readonly will do for you. 
&lt;/p&gt;
&lt;pre&gt;
static readonly int [] constIntArray = new int[] {1, 2, 3};
&lt;/pre&gt;
&lt;/p&gt;
The constIntArray field will be initialized before it its first use.
&lt;/p&gt;&lt;p&gt;
If, on the other hand, you really need a const set of values (say as an argument to an attribute constructor), then - if you can limit yourself to integral types - an enum would serve you well.
&lt;/p&gt;&lt;p&gt;
For example:
&lt;/p&gt;
&lt;pre&gt;
[Flags]
public enum Role
{
	Administrator = 1,
	BackupOperator = 2,
	// etc. 
}

public class RoleAttribute : Attribute
{
	public RoleAttribute()
	{
		CreateRole = DefaultRole;
	}

	public RoleAttribute(Role role)
	{
		CreateRole = role;
	}

	public Role CreateRole
	{
		get { return this.createRole; }
		set { this.createRole = value; }
	}

	private Role createRole = 0;
	public const Role DefaultRole = Role.Administrator
	 | Role.BackupOperator;
}

[RoleAttribute(RoleAttribute.DefaultRole)]
public class DatabaseAccount
{
	//.............. 
}
&lt;/pre&gt;
&lt;p&gt;
RoleAttribute, instead of taking an array, would only take a single argument of flags (appropriately or-ed). If the underlying type of the Role enum is long or ulong, that gives you 64 different Roles.
&lt;/p&gt;&lt;p&gt;
[Author: SantoshZ]
&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=274417" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+Language+and+Compiler/default.aspx">C# Language and Compiler</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Tips/default.aspx">Tips</category></item><item><title>How do I get and set Environment variables?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/12/02/273785.aspx</link><pubDate>Thu, 02 Dec 2004 17:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:273785</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/273785.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=273785</wfw:commentRss><description>&lt;p align="left"&gt;&lt;span id="qd_lblAnswer" style="FONT-SIZE: x-small"&gt;&lt;font face="Arial" size="3"&gt;Use the &lt;b&gt;System.Environment&lt;/b&gt; class.&lt;br /&gt;Specifically the &lt;b&gt;GetEnvironmentVariable&lt;/b&gt; and &lt;b&gt;SetEnvironmentVariable&lt;/b&gt; methods.&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;font face="Arial"&gt;Admitedly, this is not a question specific to C#, but it is one I have seen enough C# programmers ask, and the ability to &lt;em&gt;set &lt;/em&gt;environment variables is new to the Whidbey release, as is the &lt;b&gt;EnvironmentVariableTarget&lt;/b&gt; enumeration which lets you separately specify process, machine, and user. &lt;/font&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;font face="Arial"&gt;Brad Abrams blogged on &lt;a href="http://weblogs.asp.net/brada/archive/2004/01/03/50984.aspx"&gt;this&lt;/a&gt; way back at the start of this year, and followed up with a&amp;nbsp;&lt;a href="http://weblogs.asp.net/brada/archive/2004/02/23/78870.aspx"&gt;solution &lt;/a&gt;for pre-Whidbey users.&lt;/font&gt;&lt;/p&gt; &lt;p align="left"&gt;&lt;font face="Arial"&gt;[Author: SantoshZ]&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=273785" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/.NET+Framework/default.aspx">.NET Framework</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/Tips/default.aspx">Tips</category></item><item><title>Is it possible to output the command-line used to build a project in Visual Studio?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/11/30/272587.aspx</link><pubDate>Wed, 01 Dec 2004 01:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:272587</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/272587.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=272587</wfw:commentRss><description>&lt;P&gt;Now that Whidbey has been out in Beta for more than a few months, it seems worth revisiting some frequently asked questions which have different (better?) answers now. &lt;/P&gt;
&lt;P&gt;In Everett (v7.1) the answer used to be No. &lt;/P&gt;
&lt;P&gt;However, in Whidbey (v8.0), the answer is Yes (and No). &lt;/P&gt;
&lt;P&gt;For the yes part of the answer, after building, go to the Output Window, select "Show Output from: Build", and about half way down you will see a section like this: &lt;/P&gt;&lt;code&gt;
&lt;P&gt;Target "Compile" in project "ConsoleApplication1.csproj" &lt;/P&gt;
&lt;P&gt;Task "Csc" &lt;/P&gt;
&lt;P&gt;Csc.exe /noconfig /warn:4 /define:DEBUG;TRACE /debug+ /optimize- /out:obj\Debug\ConsoleApplication1.exe /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.31125\System.Data.dll, C:\WINDOWS.0\Microsoft.NET\Framework\v2.0.31125\System.XML.dll, C:\WINDOWS\Microsoft.NET\Framework\v2.0.31125\System.dll /target:exe /win32icon:App.ico AssemblyInfo.cs Class1.cs &lt;/P&gt;
&lt;P&gt;The task is invoking the IDE's in-process compiler to perform the equivalent of the above command-line. &lt;/P&gt;&lt;/code&gt;
&lt;P&gt;Now for the no part of the answer. The project system does not actually execute this command line as part of the build process. As the output says, the IDE directly calls its own in-process compiler to perform the equivalent. However, in all cases, you should get the same results using the command line suggested in the output window. If you don't, you could be looking at a bug. &lt;/P&gt;
&lt;P&gt;Note: before you cut and paste the build output to the command line, remember to add the path to CSC.EXE &lt;/P&gt;
&lt;P&gt;[Author: SantoshZ] &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=272587" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/IDE/default.aspx">IDE</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/C_2300_+Language+2.0/default.aspx">C# Language 2.0</category></item><item><title>How can I subscribe to events exposed by a remoted object?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/04/28/122691.aspx</link><pubDate>Thu, 29 Apr 2004 04:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:122691</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/122691.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=122691</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Delegates require information about the type that the method is associated with in order to make a call.&amp;nbsp; In a single app-domain, this isn't a problem, because the server (the object firing the events) has access to the type information for the client (which has the event handlers) by way of the delegate.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; However, during remoting, the server most likely does not have any information about the client.&amp;nbsp; If you want events from the server to fire in the client app domain, then the client must derive from &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;MarshalByRefObject&lt;/FONT&gt;&lt;FONT face=Arial&gt;.&amp;nbsp; This is required so that the server will call back into the client, as opposed to a copy of the client object that is passed to the server.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A simple way to do this is to place a copy of the client assembly in the same directory where the server directory is.&amp;nbsp; While this will work, it is not an elegant solution, as it exposes the client type unecessarily.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A more elegant solution (albiet more complex), is to have a single assembly that is referenced by both the client and the server.&amp;nbsp; This assembly will have a shim class which will expose methods that share the signature of the events that you want to shim.&amp;nbsp; The assembly will also provide an interface with methods that share the signature of the events as well.&amp;nbsp; The shim will then take a reference to the interface, and the methods will aggregate the call to the interface that was passed in (I recommend the constructor as the place to pass the interface implementation).&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Finally, the client object will implement the interface.&amp;nbsp; When subscribing to the events that the server exposes, attach the delegates to the methods on the shim, which will be passed your client object's implementation.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; It is also important to note that in order for this to work, the &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;TypeFilterLevel&lt;/FONT&gt;&lt;FONT face=Arial&gt; property on the sink provider for the server needs to be set to &lt;FONT face="Courier New"&gt;TypeFilterLevel.Full&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Arial&gt;.&lt;FONT size=2&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=122691" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category></item><item><title>Why does my Windows Form project not use visual styles in XP even when I call Application.EnableVisualStyles?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/04/28/122673.aspx</link><pubDate>Thu, 29 Apr 2004 04:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:122673</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/122673.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=122673</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If you set a property on a Windows Forms control which forces the creation of the control (e.g. the &lt;FONT face="Courier New"&gt;SelectedIndex&lt;/FONT&gt; property on the &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;ComboBox&lt;/FONT&gt;&lt;FONT face=Arial&gt; class), the control (and perhaps the rest of the form) will not render with visual styles enabled.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The resolution is to place the code that sets these properties in an event handler for the &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;Load&lt;/FONT&gt;&lt;FONT face=Arial&gt; event on the form/control.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=122673" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category></item><item><title>How can I update my user interface from a thread that did not create it?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx</link><pubDate>Thu, 18 Mar 2004 03:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:91685</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>21</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/91685.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=91685</wfw:commentRss><description>&lt;P&gt;When performing any action on a control which requires the updating of a user interface element (e.g. setting the &lt;CODE&gt;Text&lt;/CODE&gt; property on almost any class derived from Control, updating the data source behind a DataGrid), these operations MUST take place on the thread that created the UI element. &lt;/P&gt;
&lt;P&gt;In order to do this, the &lt;CODE&gt;Control&lt;/CODE&gt; class provides the &lt;CODE&gt;Invoke&lt;/CODE&gt; method, which will take a delegate and execute it on the thread that the UI element was created on. In order to use this, one must declare a function that performs the UI operation. For example, say a form has a TextBox on it named &lt;CODE&gt;m_TextBox&lt;/CODE&gt;. To update the text from another thread, create a method that will update the &lt;CODE&gt;Text&lt;/CODE&gt; property on the TextBox: &lt;PRE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;FONT color=green&gt;// The declaration of the textbox.&lt;/FONT&gt;
&lt;FONT color=blue&gt;private&lt;/FONT&gt; TextBox m_TextBox;

&lt;FONT color=green&gt;// Updates the textbox text.&lt;/FONT&gt;
&lt;FONT color=blue&gt;private&lt;/FONT&gt; &lt;FONT color=blue&gt;void&lt;/FONT&gt; UpdateText(&lt;FONT color=blue&gt;string&lt;/FONT&gt; text)
{
  &lt;FONT color=green&gt;// Set the textbox text.&lt;/FONT&gt;
  m_TextBox.Text = text;
}
&lt;/PRE&gt;
&lt;P&gt;Now, create a delegate that has the same signature as the method that was previously defined: &lt;/P&gt;&lt;PRE&gt;&lt;FONT color=blue&gt;public&lt;/FONT&gt; &lt;FONT color=blue&gt;delegate&lt;/FONT&gt; &lt;FONT color=blue&gt;void&lt;/FONT&gt; UpdateTextCallback(&lt;FONT color=blue&gt;string&lt;/FONT&gt; text);
&lt;/PRE&gt;
&lt;P&gt;In your thread, you can call the &lt;CODE&gt;Invoke&lt;/CODE&gt; method on &lt;CODE&gt;m_TextBox&lt;/CODE&gt;, passing the delegate to call, as well as the parameters. &lt;/P&gt;&lt;PRE&gt;m_TextBox.Invoke(&lt;FONT color=blue&gt;new&lt;/FONT&gt; UpdateTextCallback(&lt;FONT color=blue&gt;this&lt;/FONT&gt;.UpdateText), &lt;BR&gt;            &lt;FONT color=blue&gt;new&lt;/FONT&gt; &lt;FONT color=blue&gt;object&lt;/FONT&gt;[]{&amp;#8221;Text generated on non-UI thread.&amp;#8221;});
&lt;/PRE&gt;
&lt;P&gt;Note: Do not create a method that matches the EventHandler delegate signature and pass that. The implementation of Invoke on the Control class will not take into account the parameters passed to Invoke if the type of the delegate is EventHandler. It will pass the control that &lt;CODE&gt;Invoke&lt;/CODE&gt; was called on for the sender parameter as well as the value returned by &lt;CODE&gt;EventArgs.Empty&lt;/CODE&gt; for the &lt;CODE&gt;e&lt;/CODE&gt; parameter. &lt;/P&gt;
&lt;P&gt;[Author: Nicholas Paldino] &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=91685" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category></item><item><title>Where can I find sample C# code for simple threading?</title><link>http://blogs.msdn.com/csharpfaq/archive/2004/03/15/90082.aspx</link><pubDate>Mon, 15 Mar 2004 23:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:90082</guid><dc:creator>CSharpFAQ</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/csharpfaq/comments/90082.aspx</comments><wfw:commentRss>http://blogs.msdn.com/csharpfaq/commentrss.aspx?PostID=90082</wfw:commentRss><description>&lt;P&gt;Refer to the System.Threading namespace on &lt;A href="http://msdn.microsoft.com/"&gt;MSDN&lt;/A&gt; for full details. Meanwhile here is a quick taste. &lt;/P&gt;&lt;PRE&gt;&lt;FONT color=blue&gt;using&lt;/FONT&gt; System; 
&lt;FONT color=blue&gt;using&lt;/FONT&gt; System.Threading; 

&lt;FONT color=blue&gt;class&lt;/FONT&gt; ThreadTest 
{ 
    &lt;FONT color=blue&gt;public&lt;/FONT&gt; &lt;FONT color=blue&gt;void&lt;/FONT&gt; Runme() 
    { 
        Console.WriteLine(&lt;FONT color=red&gt;"Runme Called"&lt;/FONT&gt;); 
        Thread.Sleep(10000); 
    }

    &lt;FONT color=blue&gt;public&lt;/FONT&gt; &lt;FONT color=blue&gt;static&lt;/FONT&gt; &lt;FONT color=blue&gt;void&lt;/FONT&gt; Main(String[] args)
    { 
        ThreadTest b = &lt;FONT color=blue&gt;new&lt;/FONT&gt; ThreadTest(); 
        Thread t = &lt;FONT color=blue&gt;new&lt;/FONT&gt; Thread(&lt;FONT color=blue&gt;new&lt;/FONT&gt; ThreadStart(b.Runme)); 
        t.Start(); 

        Console.WriteLine(&lt;FONT color=red&gt;"Thread 't' started."&lt;FONT color=#000000&gt;);&lt;BR&gt;        Console.WriteLine(&lt;FONT color=red&gt;"&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=red&gt;There is no telling when " + &lt;BR&gt;                          &lt;/FONT&gt;&lt;FONT color=red&gt;"'Runme' will be invoked. "&lt;/FONT&gt;); 

        t.Join(); 

        Console.WriteLine(&lt;FONT color=red&gt;"Thread 't' has ended."&lt;/FONT&gt;); 
    }
}
&lt;/PRE&gt;&lt;PRE&gt;[Author: Santosh Zachariah]&lt;/PRE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=90082" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/csharpfaq/archive/tags/General/default.aspx">General</category></item></channel></rss>