March 2004 - Posts

When should I use == and when should I use Equals?
29 March 04 11:56 PM
The Equals method is just a virtual one defined in System.Object , and overridden by whichever classes choose to do so. The == operator is an operator which can be overloaded by classes, but which usually has identity behaviour. For reference types where Read More...
Postedby CSharpFAQ | 19 Comments    
Should I assign null to my local variables?
26 March 04 08:15 PM
Q: Should I assign null to my local variables after I use them? For example: string s = ...; Console.WriteLine(s); s = null; A: There is rarely a need to do this for local variables in C# The lifetime of variables is tracked by the JIT - it analyzes how Read More...
Postedby CSharpFAQ | 13 Comments    
Why do I need a null test before I invoke a delegate?
19 March 04 08:41 PM
Q: Why do I need a null test before I invoke a delegate? A: If you have an event in a class, you need to add a null test before you call the delegate. Typically, you would write: if (Click != null ) Click(arg1, arg2); There is actually a possible race Read More...
Postedby CSharpFAQ | 12 Comments    
Why doesn't C# warn about unused methods?
19 March 04 07:45 PM
Q: Why doesn't C# warn on unused methods? A: This is something that the C# compiler could do, subject to a few caveats: Virtual functions would have to be excluded Interface implementations wouldn have to be excluded You would get false positives if you Read More...
Postedby CSharpFAQ | 5 Comments    
How can I update my user interface from a thread that did not create it?
17 March 04 08:58 PM
When performing any action on a control which requires the updating of a user interface element (e.g. setting the Text property on almost any class derived from Control, updating the data source behind a DataGrid), these operations MUST take place on Read More...
Postedby CSharpFAQ | 19 Comments    
Filed under:
Why can't I have static and instance methods with the same name?
16 March 04 08:39 PM
Q: Why can't I have static and instance methods with the same name? class Test { static void Process(); void Process(); void AmbiguousCaller() { Process(); } } there's no ambiguity, since the first can only be called through the type name, and the second Read More...
Postedby CSharpFAQ | 12 Comments    
Where can I find sample C# code for simple threading?
15 March 04 04:02 PM
Refer to the System.Threading namespace on MSDN for full details. Meanwhile here is a quick taste. using System; using System.Threading; class ThreadTest { public void Runme() { Console.WriteLine( "Runme Called" ); Thread.Sleep(10000); } public static Read More...
Postedby CSharpFAQ | 11 Comments    
Filed under:
How do C# generics compare to C++ templates?
12 March 04 08:58 PM
Q: How do C# generics compare to C++ templates? A: This is really a fairly complex topic. Anders has touched on it in an interview . I should state at the outset that the goals of generics are not the same as the goals of templates. There are some things Read More...
Postedby CSharpFAQ | 26 Comments    
Where can I get a full comparison between C# and VB.NET?
12 March 04 02:31 AM
Microsoft provides a very full language equivalents page which compares not only C# and VB.NET, but also other languages targeted at the .NET framework. It looks at the equivalent concepts, keywords, types, operators etc. A very valuable resource when Read More...
Postedby CSharpFAQ | 6 Comments    
Is there an equivalent of MyClass?
12 March 04 02:30 AM
No, C# doesn't have an equivalent of VB.NET's MyClass keyword. If you want to guarantee not to call an overridden version of a method, you need to make it non-virtual in the first place. [Author: Jon Skeet] Read More...
Postedby CSharpFAQ | 8 Comments    
What do I use instead of addressof?
12 March 04 02:29 AM
To create delegate instances in C#, you just specify the delegate type, the method, and (if you want to create a delegate targetting a different instance or type from the current one) the target. For instance, each of these creates a ThreadStart delegate: Read More...
Postedby CSharpFAQ | 1 Comments    
How do I get the rightmost part of a string, as with the VB Right function?
12 March 04 02:28 AM
Use String.Substring . Assuming that x is a string of length at least n , to get the last n characters, you would use x.Substring(x.Length-n) . Note that the above assumes that the string is at least n characters long. For a more robust version, you might Read More...
Postedby CSharpFAQ | 10 Comments    
What are the equivalents of Me and MyBase?
12 March 04 02:27 AM
Me in C# is this , and MyBase in C# is base . To access normal members, just use this.memberName or base.memberName . For information about chaining constructors together, see my article on constructors . [Author: Jon Skeet] Read More...
Postedby CSharpFAQ | 0 Comments    
What's the equivalent of Nothing?
12 March 04 02:26 AM
For reference types, the equivalent of VB's Nothing is C#'s null . For value types, it's the default value - 0 , false , etc. [Author: Jon Skeet] Read More...
Postedby CSharpFAQ | 4 Comments    
How do I tell C# what kind of literal number I want?
12 March 04 02:18 AM
If you need to tell C# that you want it to treat a literal as a particular type of number, you may do so by adding a number type suffix at the end of the literal you provide. For example: 1u; // An unsigned int 1l; // A signed long 1ul; // An unsigned Read More...
Postedby CSharpFAQ | 5 Comments    
How do I use an alias for a namespace or class?
12 March 04 02:16 AM
Use the using directive to create an alias for a long namespace or class name. You can then use it anywhere you normally would have used that class or namespace. The using alias has a scope within the namespace you declare it in. Sample code: // Namespace: Read More...
Postedby CSharpFAQ | 2 Comments    
What's the difference between override and new?
12 March 04 01:30 AM
This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived Read More...
Postedby CSharpFAQ | 10 Comments    
Why doesn't C# have checked exceptions?
12 March 04 12:23 AM
Checked exceptions are a very hotly debated topic in some circles, particularly for experienced Java developers moving to, or additionally learning, C#. Here are some resources that discuss the issue in depth: The Trouble With Checked Exceptions (Anders Read More...
Postedby CSharpFAQ | 5 Comments    
What's the difference between cast syntax and using the as operator?
12 March 04 12:22 AM
Using the as operator differs from a cast in C# in three important ways: It returns null when the variable you are trying to convert is not of the requested type or in it's inheritance chain, instead of throwing an exception. It can only be applied to Read More...
Postedby CSharpFAQ | 4 Comments    
What's the difference between string and System.String?
12 March 04 12:20 AM
C# defines a number of aliases for CLR types. They may be used interchangably, and even mixed together, e.g. string x = new System.String(' ', 5); . These are the aliases defined: Alias CLR type string System.String sbyte System.SByte byte System.Byte Read More...
Postedby CSharpFAQ | 5 Comments    
What's the difference between an event and a delegate?
12 March 04 12:18 AM
Put simply, an event gives more limited access than a delegate. If an event is made public, code in other classes can only add or remove handlers for that event; they can't necessarily fire it, find out all the handlers for it, or remove handlers they Read More...
Postedby CSharpFAQ | 1 Comments    
Why can't I use static and const together?
12 March 04 12:17 AM
All constants declarations are implicitly static, and the C# specification states that the (redundant) inclusion of the static modifier is prohibited. I believe this is to avoid the confusion which could occur if a reader were to see two constants, one Read More...
Postedby CSharpFAQ | 1 Comments    
What character escape sequences are available?
12 March 04 12:15 AM
C# defines the following character escape sequences: \' - single quote, needed for character literals \" - double quote, needed for string literals \\ - backslash \0 - Unicode character 0 \a - Alert (character 7) \b - Backspace (character 8) \f - Form Read More...
Postedby CSharpFAQ | 7 Comments    
What does an @ before the start of a string literal mean?
12 March 04 12:10 AM
A string literal such as @"c:\Foo" is called a verbatim string literal . It basically means, "don't apply any interpretations to characters until the next quote character is reached". So, a verbatim string literal can contain backslashes (without them Read More...
Postedby CSharpFAQ | 4 Comments    
How are parameters passed in C#? Are they passed by reference or by value?
11 March 04 02:17 AM
By default, all parameters are passed by value in C#. Parameters are only passed by reference if you explicitly include an out or ref modifier. However, you need to be aware that when the type of the parameter is a reference type, you're passing a reference Read More...
Postedby CSharpFAQ | 0 Comments    
Does C# have templates like C++?
11 March 04 02:13 AM
Although C# doesn't have templates, and isn't likely to get them, it is getting a feature called generics which will be available in the next version of .NET and Visual Studio. Generics will be a feature in the CLR itself, and most languages targetting Read More...
Postedby CSharpFAQ | 2 Comments    
Why doesn't C# have VB.NET's 'with' operator?
11 March 04 02:02 AM
Many people, including the C# language designers, believe that 'with' often harms readability, and is more of a curse than a blessing. It is clearer to declare a local variable with a meaningful name, and use that variable to perform multiple operations Read More...
What are the advantages of C# over VB.NET and vice versa?
11 March 04 01:59 AM
The choice between C# and VB.NET is largely one of subjective preference. Some people like C#'s terse syntax, others like VB.NET's natural language, case-insensitive approach. Both have access to the same framework libraries. Both will perform largely Read More...
Where is the C# specification?
11 March 04 01:56 AM
There are two versions of the C# specification - one from Microsoft, and one from ECMA. They are the same in all important respects (a few pieces of explanatory wording are different, but nothing that affects the specification itself) but the numbering Read More...
Postedby CSharpFAQ | 2 Comments    
Why don't relational operators return bool as a type?
09 March 04 07:30 PM
Section 10.9.2 of the language specification says: A binary operator must take two parameters, at least one of which must have the class or struct type in which the operator is declared. Parameters of the shift operators ( § 7.8 ) are further constrained. Read More...
Postedby CSharpFAQ | 1 Comments    
Why doesn't C# support #define macros?
09 March 04 06:30 PM
In C++, I can define a macro such as: #define PRODUCT(x, y, z) x * y * z and then use it in code: int a = PRODUCT(3, 2, 1); C# doesn't allow you to do this. Why? There are a few reasons why. The first is one of readability. One of our main design goals Read More...
Postedby CSharpFAQ | 15 Comments    
Why don't namespace using directives import nested namespaces?
07 March 04 01:09 PM
In C#, when you specify a “using” clause, such as using System.Text; the compiler only imports the types in System.Text into the global namespace - it doesn't do the same with any namespaces inside of System.Text. So, while that using allows Read More...
Postedby CSharpFAQ | 5 Comments    
Why doesn't C# have a power operator?
07 March 04 12:54 PM
Some languages provide a power operator, so one can write something like: float result = value^2; rather than having to resort to calling. We don't have one in C#. It would be possible to add a power operator to the language, but performing this operation Read More...
Postedby CSharpFAQ | 7 Comments    
Why doesn't C# support multiple inheritance?
07 March 04 12:47 PM
This answer is from Chris Brumme via the following post . I've copied the text in here in case the post disappears. *** There are a number of reasons we don't implement Multiple Implementation Inheritance directly. (As you know, we support Multiple Interface Read More...
Postedby CSharpFAQ | 16 Comments    
Why doesn't C# support default parameters?
07 March 04 12:40 PM
Update: Named and optional (default) parameters are available starting from C# 4.0. For more information, see Named and Optional Arguments (C# Programming Guide) . In languages such as C++, a default value can be included as part of the method declaration: Read More...
Postedby CSharpFAQ | 30 Comments    
Ask a FAQ Question
06 March 04 02:47 PM
Do you have a FAQ about: The C# Language or compiler The C# IDE The Visual Studio debugger Reply to this post, and your question will be added to our list to be answered. Of course, depending on the nature of your comment, and the volume we receive, we Read More...
Postedby CSharpFAQ | 188 Comments    

This Blog

Syndication

Page view tracker