Sign In
C# Frequently Asked Questions
The C# team posts answers to common questions and describes new language features
Translate This Page
Translate this page
Powered by
Microsoft® Translator
Options
Email Blog Author
RSS for posts
Atom
RSS for comments
OK
Search
Advanced search options...
Search In:
Everything
Blogs
Forums
People
Groups
Places
Pages
Date range:
All Time
Last Year
Last 6 Months
Last 3 Months
Last Month
Last Week
Last Two Days
Tags
.NET Framework
.NET Framework 4
async
asynchronous programming
await
C#
C# 3.0
C# 4.0
C# compiler
C#/VB.NET Equivalents
DLR
dynamic language runtime
expression trees
IDE
parallel-processing
parallel-programming
reflection
roslyn
Task Parallel Library
Tips
TPL
Visual Studio
Visual Studio 2010
Visual Studio Async CTP
WPF
Archive
Archives
April 2012
(1)
February 2012
(3)
January 2012
(2)
December 2011
(1)
November 2011
(3)
October 2011
(1)
August 2011
(2)
April 2011
(1)
March 2011
(1)
February 2011
(1)
November 2010
(1)
October 2010
(1)
September 2010
(1)
August 2010
(1)
July 2010
(2)
June 2010
(2)
May 2010
(1)
April 2010
(2)
March 2010
(1)
February 2010
(1)
January 2010
(2)
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)
March, 2004
MSDN Blogs
>
C# Frequently Asked Questions
>
March, 2004
Posts
Subscribe via RSS
Sort by:
Most Recent
|
Most Views
|
Most Comments
Excerpt View
|
Full Post View
C# Frequently Asked Questions
When should I use == and when should I use Equals?
Posted
over 8 years ago
by
CSharpFAQ
19
Comments
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...
C# Frequently Asked Questions
Should I assign null to my local variables?
Posted
over 8 years ago
by
CSharpFAQ
14
Comments
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...
C# Frequently Asked Questions
Why do I need a null test before I invoke a delegate?
Posted
over 8 years ago
by
CSharpFAQ
12
Comments
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...
C# Frequently Asked Questions
Why doesn't C# warn about unused methods?
Posted
over 8 years ago
by
CSharpFAQ
6
Comments
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...
C# Frequently Asked Questions
How can I update my user interface from a thread that did not create it?
Posted
over 8 years ago
by
CSharpFAQ
33
Comments
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...
C# Frequently Asked Questions
Why can't I have static and instance methods with the same name?
Posted
over 8 years ago
by
CSharpFAQ
12
Comments
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...
C# Frequently Asked Questions
Where can I find sample C# code for simple threading?
Posted
over 8 years ago
by
CSharpFAQ
11
Comments
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...
C# Frequently Asked Questions
How do C# generics compare to C++ templates?
Posted
over 8 years ago
by
CSharpFAQ
28
Comments
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...
C# Frequently Asked Questions
Where can I get a full comparison between C# and VB.NET?
Posted
over 8 years ago
by
CSharpFAQ
6
Comments
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...
C# Frequently Asked Questions
Is there an equivalent of
MyClass
?
Posted
over 8 years ago
by
CSharpFAQ
8
Comments
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]
C# Frequently Asked Questions
What do I use instead of
addressof
?
Posted
over 8 years ago
by
CSharpFAQ
1
Comments
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...
C# Frequently Asked Questions
How do I get the rightmost part of a string, as with the VB
Right
function?
Posted
over 8 years ago
by
CSharpFAQ
10
Comments
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...
C# Frequently Asked Questions
What are the equivalents of
Me
and
MyBase
?
Posted
over 8 years ago
by
CSharpFAQ
0
Comments
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]
C# Frequently Asked Questions
What's the equivalent of
Nothing
?
Posted
over 8 years ago
by
CSharpFAQ
4
Comments
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]
C# Frequently Asked Questions
How do I tell C# what kind of literal number I want?
Posted
over 8 years ago
by
CSharpFAQ
5
Comments
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;...
C# Frequently Asked Questions
How do I use an alias for a namespace or class?
Posted
over 8 years ago
by
CSharpFAQ
5
Comments
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: ...
C# Frequently Asked Questions
What's the difference between
override
and
new
?
Posted
over 8 years ago
by
CSharpFAQ
16
Comments
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...
C# Frequently Asked Questions
Why doesn't C# have checked exceptions?
Posted
over 8 years ago
by
CSharpFAQ
5
Comments
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...
C# Frequently Asked Questions
What's the difference between cast syntax and using the
as
operator?
Posted
over 8 years ago
by
CSharpFAQ
4
Comments
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...
C# Frequently Asked Questions
What's the difference between
string
and
System.String
?
Posted
over 8 years ago
by
CSharpFAQ
10
Comments
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 ...
C# Frequently Asked Questions
What's the difference between an event and a delegate?
Posted
over 8 years ago
by
CSharpFAQ
1
Comments
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...
C# Frequently Asked Questions
Why can't I use
static
and
const
together?
Posted
over 8 years ago
by
CSharpFAQ
0
Comments
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...
C# Frequently Asked Questions
What character escape sequences are available?
Posted
over 8 years ago
by
CSharpFAQ
19
Comments
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...
C# Frequently Asked Questions
What does an @ before the start of a string literal mean?
Posted
over 8 years ago
by
CSharpFAQ
4
Comments
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...
C# Frequently Asked Questions
How are parameters passed in C#? Are they passed by reference or by value?
Posted
over 8 years ago
by
CSharpFAQ
1
Comments
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...
Page 1 of 2 (36 items)
1
2