C# Frequently Asked Questions
The C# team posts answers to common questions and describes new language features
Browse by Tags
All Tags
»
C# Language and Compiler
(RSS)
.NET Framework
C# Language 2.0
C#/VB.NET Equivalents
General
IDE
Tips
Where can I find design and coding guidelines for .NET?
21 February 05 01:30 PM
tipu_77 asked "What are microsoft suggested naming conventions in C#?" The .NET Framework Team collects their recommendations at their GotDotNet community site . which points to a fairly comprehensive MSDN page Design Guidelines for Class Library Developers
Read More...
What does the /target: command line option do in the C# compiler?
04 December 04 11:33 PM
All the /target: options except module create .NET assemblies. Depending on the option, the compiler adds metadata for the operating system to use when loading the portable executable (PE) file and for the runtime to use in executing the contained assembly
Read More...
What is the difference between const and static readonly?
03 December 04 05:13 PM
The difference is that the value of a static readonly field is set at run time, and can thus be modified by the containing class, w hereas the value of a const field is set to a compile time constant. In the static readonly case, the containing class
Read More...
How do I create a constant that is an array?
03 December 04 09:33 AM
Strictly speaking you can't, since const can only be applied to a field or local whose value is known at compile time. In both the lines below, the right-hand is not a constant expression (not in C#). const int [] constIntArray = newint [] {2, 3, 4};
Read More...
How to keep a local variable in scope across a try and catch block?
12 August 04 06:33 PM
The following code won't work, because conn goes out of scope before you enter the catch block. try { Connection conn = new Connection(); conn.Open(); } catch { if (conn != null ) conn.Close(); } The fix is simple - just declare conn before entering the
Read More...
Why does my switch statement works differently?
22 July 04 05:50 PM
C# does not support an explicit fall through for case blocks (unless the block is empty) For an explanation of why, see Why is the C# switch statement designed to not allow fall through, but still require a break? on MSDN The following code is not legal
Read More...
Why does C#'s iterators feature spit out a class definition instead of a struct definition?
20 July 04 10:30 AM
Q: Why does C#'s iterators feature spit out a class definition instead of a struct definition? The iterators feature in C# generates classes that implement the enumerators required. This is detailed in the C# Specification . Why doesn't it use structs,
Read More...
How do I cast a string to an int, float, double, etc?
30 May 04 12:08 PM
You can use the Convert class or the Parse method of the built-in type you are casting to. i.e. string myStr = "123"; int myParsedInt = Int32.Parse(myStr); int myConvertedInt = Convert.ToInt32(myStr); This example uses the int type, but you can also use
Read More...
How are return values from a delegate handled?
27 May 04 03:50 PM
Q: How are multiple return values from a delegate handled? In C#, it's possible to write a delegate such as: delegate double GetResult(params double p); If there is more than one method on this delegate, there are multiple return values. A: In this situation,
Read More...
Why can't I use the same variable as an inner loop does?
18 May 04 02:59 PM
Q: Why can't I do the following: namespace Bug { class Class1 { [ STAThread ] static void Main ( string [] args ) { for ( int i = 0; i < 100; i ++ ) { } int i = 0; } } } the compiler gives me an error on the “int i = 0;” part of the code.
Read More...
How do I write a method that accepts a variable number of parameters?
13 May 04 03:44 PM
Q: How do I write a method that accepts a variable number of parameters? A: Languages like C and C++ have always offered some means for using and creating functions capable to accept a variable number of parameters. The most widely known example of a
Read More...
Why doesn't C# support static method variables?
11 May 04 09:41 PM
Q: In C++, it's possible to write a static method variable, and have a variable that can only be accessed from inside the method. C# doesn't provide this feature. Why? A: There are two reasons C# doesn't have this feature. First, it is possible to get
Read More...
Why must attribute properties be read/write in C#?
11 May 04 09:16 PM
Q: Why must attribute properties be read/write in C#? In the C# language, when you define an attribute class, you can define both constructor arguments and properties. For example: class MyAttribute: Attribute { string name; int number; public MyAttribute(string
Read More...
Why do I get the error "Object reference not set to an instance of an object"?
06 May 04 09:01 PM
The code is trying to access a member of a reference type variable that is set to null . Given the following class, let's examine some code that could be in the Main method: using System; class Foo { static void Main() { // foo has not been instantiated
Read More...
Why did I receive the error: "The type or namespace '<namespace name>' does not exist in the class or namespace '<parent namespace>' (are you missing an assembly reference?)"
29 April 04 09:09 AM
You need to add a reference in your project to an assembly where that namespace is defined. If you are using VS.NET: 1. Right click on the References folder on your project. 2. Select Add Reference. 3. Select the .NET tab (or select the Browse button
Read More...
More Posts
Next page »
This Blog
Home
Links
Email
Tags
.NET Framework
.NET Framework 4.0
C# 4.0
C# Language 2.0
C# Language and Compiler
C#/VB.NET Equivalents
Debugger/Debugging
DLR
dynamic language runtime
DynamicObject
ExpandoObject
expression trees
General
IDE
LINQ
LINQ to Objects
Tips
Visual Studio 2010
Archives
November 2009 (1)
October 2009 (2)
September 2009 (1)
March 2009 (1)
January 2009 (1)
October 2006 (2)
March 2006 (3)
February 2005 (1)
December 2004 (4)
November 2004 (1)
October 2004 (15)
August 2004 (3)
July 2004 (3)
June 2004 (1)
May 2004 (8)
April 2004 (4)
March 2004 (36)
Syndication
RSS 2.0
Atom 1.0