C# Frequently Asked Questions
The C# team posts answers to common questions and describes new language features
May 2004 - Posts
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 don't nullable relational operators return bool? instead of bool?
25 May 04 11:51 AM
Q: Why don't nullable relational operators return “bool?” instead of “bool“? Nullable types are a new feature of C# 2.0 . When dealing with nullable types, if you write the following: int? i = 15; int? j = 16; the type of i ==
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...
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