Hi, here are answers for some of the questions I suggested a short time ago
for the m.p.d.l.c FAQ Jon Skeet is organizing. It was Jon's intention that
these be 'honed' by the group before being included - so hone away. It's my
intention that you merely not 'tear me a new one' for any inaccuracies in my
answers. I give credit to Anders Hejlsberg for answering my question about
the non-int operators a short while ago in this thread (which is the basis
for my answer below):
http://www.gotdotnet.com/Community/M...aspx?id=157575
- what's the difference between using cast syntax and the 'as' operator? (C#
as a language)
Using the 'as' operator differs from a cast in C# in three important ways:
1. It returns a null when the variable you are trying to convert is not of
the requested type or in it's inheritance chain
2. It can only be applied to reference type variables converting to
reference types.
3. Using 'as' will not perform user-defined conversions, such as implicit or
explicit conversion operators, which casting syntax will do.
There are in fact two completely different operations defined in IL that
handle these two keywords (the castclass and isinst instructions) - it's not
just 'syntactic sugar' written by C# to get this different behavior. The
'as' operator appears to be slightly faster in v1.0 and v1.1 of Microsoft's
CLR compared to casting (even in cases where there are no invalid casts
which would severely lower casting's performance due to exceptions).
- how do I use an alias for a namespace or class? (C# as a language)
Use the 'using' directive to create an alias for a long namespace or class.
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.
// namespace:
using act = System.Runtime.Remoting.Activation;
// class
using list = System.Collections.ArrayList;
- why doesn't C# have checked exceptions? (C# as a language??)
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:
http://www.artima.com/intv/handcuffs.html
http://www.mindview.net/Etc/Discussi...ckedExceptions
- why are struct constructors in C# required to have at least one argument?
(C# as a language)
The .NET runtime can't guarantee that parameterless constructors will be
called. If structs where to allow default, parameterless constructors, it
would imply that these default constructors would *always* be called.
However, the runtime can not make this guarantee. For example an array of
value types will be initialized to the initial values of it's members (i.e.
0 for number type primitive members, null for reference types etc) *NOT to
the values provided in a default constructor* - which makes structs better
performing by not having to call constructor code. Enforcing a minimum of
one parameter in the constructor reduces the possibility that someone will
define a constructor that they then expect to be called every time one of
their struct types is constructed.
- how can I show an int as a binary number - a string of 1's and 0's
The convert class has an overload of the static ToString() method that takes
two ints and returns a string populated with the number in the specified
base.
Convert.ToString(128, 2);
- why does C# return an int when you add two bytes/shorts/sbytes/ushorts
together?
C# always promotes 8 and 16 bit values to 32 bits before performing
arithmetic operations. It is actually less efficient to emulate 8 or 16 bit
arithmetic on a 32 bit processor than it is to just do the operations in 32
bits.
C# narrowing conversions (conversions that might affect the magnitude of a
number like casting an int to short) are always explicit. By contrast they
can occur implicitly in C/C++. That's why you have to cast the result of
expressions like ushort + ushort back to ushort in an assignment.
- how can I speed up P/Invoke calls made through C# to external functions?
One easy way is to apply the SuppressUnmanagedCodeSecurityAttribute to the
method, class or interface that you are making platform invoke calls
through. Apply this attribute bypasses the runtime security check that
ensures that the code calling the external method has UnmanagedCode
permission. This can cause a serious security violation in your application
and using it means that securing this unmanaged call is the responsibility
of the developer, not the framework.
example:
[System.Security.SuppressUnmanagedCodeSecurityAttri bute()]
[DllImport("User32.dll")]
public extern static int GetCursorPos( ref Point thePoint );
- why doesn't calling Initialize() on a reference-type array fill in the
array with objects?
The System.Array class instance method Initialize() exists solely to
initialize value type arrays to their default values and is not valid on
reference type arrays. (In fact it is not even intended for C# value type
structs, as these structs can have no default constructor for Initialize to
call.)
- How can I implement a *global* hook in C#?
It is not possible because C# / .NET does not support an essential element:
a DLL export providing a consistent function pointer. See here
http://support.microsoft.com/?kbid=318804 at the bottom for a little more
detail.
- How do I tell C# what kind of literal number I want? (f, L, U etc) (C# as
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:
1U; // an unsiged int
1ul; // an unsigned long
1f; // a System.Single floating-point number;
This is somewhat important because sometimes you must match a literal to the
signature of something or specify the value to 'defeat' an implicit cast
behavior you don't like. For example this:
Hashtable names = new Hashtable(100, 0.1);
won't compile because it takes the signatures (int, float) and the above is
(int, double). The line should read:
Hashtable names = new Hashtable(100, 0.1f);
A full listing of the suffixes is in the Grammar portion of the c#
specification under C.1.8 Literals.
http://msdn.microsoft.com/library/de...harpspec_C.asp
- What's the difference between the ref and out modifiers on parameters?
Both the ref and out method parameters are applied to arguments of a method
and both mean that the argument will be passed "by reference" (either a
value type variable by reference or a reference type variable by reference).
The out parameter, however, allows you to pass in an uninitialized variable
like so and guarantees it will come back with it's value set (so long as the
called method was written in C#, anyway).
int i;
DoStuffByRef(out i);
// i is now a usable int value
--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard