The C# team posts answers to common questions
May 2004 - Posts
-
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...
|
-
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...
|
-
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...
|
-
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...
|
-
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...
|
-
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...
|
-
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...
|
-
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...
|