473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

FAQ Suggestions

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
Nov 15 '05 #1
17 1298
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:
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):
Great stuff - thanks very much for all your work! A few comments (with
questions without any comments snipped).
- 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.
I'm going to try to steer clear of using "default" for parameters
unless I'm talking about the constructor supplied by default if you
don't give one (in a reference type). I believe "parameterless" is
actually the correct term - for instance, you can't supply a default
constructor for a reference type, you just get given one if you don't
supply any constructors at all. This is one piece of terminology which
is much abused (including by Microsoft), but I believe the difference
can be useful, so I'll try to draw a distinction.

On the other hand, I've just looked at the CLR spec and that mentions
default constructors 3 times, so maybe I'm just wrong. Shame though, I
thought it was quite handy...
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.
Explaining here that CLR types *can* have parameterless constructors,
but C# can't generate such types would be a good idea. It took me a
while to work out what was meant here.
- 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.
I would personally stick to short in the example here, just so people
don't think it's got *anything* to do with being unsigned!
- 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.
It might be worth making the question a bit more explicit in terms of
what a global hook is, just for clarity. (Or put it in the answer.)
- 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;
Add
1d // a System.Double floating-point number
1m // a System.Decimal floating-point number

Then at least even if there aren't all the variants in terms of case,
all the bases are covered, as it were.
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...ry/en-us/csspe
c/html/vclrfcsharpspec_C.asp
Unless anyone particularly minds, I'd like to keep to the ECMA spec
rather than the MS spec for references. This may be impossible for 2.0
features for a while, but the FAQ can be fixed later for those features
:)
- 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


Gosh, I'm surprised I don't have that in there already. I'll include a
link to my page about parameter passing...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:
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): <snip>
- 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.
It might be worth making the question a bit more explicit in terms of
what a global hook is, just for clarity. (Or put it in the answer.)


Perhaps a more general "Why doesn't C# allow exporting functions in dlls",
especially considering that it is possible in IL(If memory serves, don't
recall how to do it). I can find the answer, just don't want to dig through
Brumme's blogs right now.

Anyway, after explaining why you can't export functions, it would be easy to
explain why xprocs, global hooks, and other such things aren't possible with
C#.
- 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;


Add
1d // a System.Double floating-point number
1m // a System.Decimal floating-point number

Then at least even if there aren't all the variants in terms of case,
all the bases are covered, as it were.
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...ry/en-us/csspe
c/html/vclrfcsharpspec_C.asp


Unless anyone particularly minds, I'd like to keep to the ECMA spec
rather than the MS spec for references. This may be impossible for 2.0
features for a while, but the FAQ can be fixed later for those features
:)


I agree on this, ECMA spec is prefereable. ALthough,is the MS spec just a
clone or is it a rewrite?(I've never actually used the msdn spec). - 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


Gosh, I'm surprised I don't have that in there already. I'll include a
link to my page about parameter passing...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Richard,
- 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.

Here I think it's worth pointing out the special case for compound
assignment operators (14.13.2).

- 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.
Well SUCSA is only one way to speed up interop code (and probably the
last one I'd recommend people to use, because of the security stuff).
I think it's more important to promote correct use of In/Out
attributes and parameter typing to avoid unnecessary marshaling, and
making "chunky" rather than "chatty" calls if possible.

- 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.


C# doesn't support it, but C++ and IL Assembler does. But getting a
function export is only part of the problem. It would be a terrible
idea to force the CLR to be loaded into every process the hook code is
run in anyway.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #4
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:

[Using the ECMA spec]
I agree on this, ECMA spec is prefereable. ALthough,is the MS spec just a
clone or is it a rewrite?(I've never actually used the msdn spec).


I believe they're *almost* word-for-word identical, but with different
examples in some places and (more importantly) different numbering.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Inline

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
Richard,
- 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 bitarithmetic on a 32 bit processor than it is to just do the operations in 32bits.

C# narrowing conversions (conversions that might affect the magnitude of anumber like casting an int to short) are always explicit. By contrast theycan 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.

Here I think it's worth pointing out the special case for compound
assignment operators (14.13.2).


Okay, I will add that.

- how can I speed up P/Invoke calls made through C# to external functions?
One easy way is to apply the SuppressUnmanagedCodeSecurityAttribute to themethod, 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 applicationand using it means that securing this unmanaged call is the responsibilityof the developer, not the framework.
Well SUCSA is only one way to speed up interop code (and probably the
last one I'd recommend people to use, because of the security stuff).
I think it's more important to promote correct use of In/Out
attributes and parameter typing to avoid unnecessary marshaling, and
making "chunky" rather than "chatty" calls if possible.


I know of no one more qualified than you to write such a thing, Mattias, if
you want to take a stab at it. Otherwise be happy to I'll give it a try.

- 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.


C# doesn't support it, but C++ and IL Assembler does. But getting a
function export is only part of the problem. It would be a terrible
idea to force the CLR to be loaded into every process the hook code is
run in anyway.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 15 '05 #6
Hello Jon and Richard,

I wasn't aware of your effort.

Have you thought of allowing your FAQs to be compiled on a Wiki server?

In my mind, the advantage would be that the FAQs can be honed by the
community (or the MVP) on an ongoing basis, not just a one-off effort.

Either way, it's a valuable effort. Thanks for doing this work.

--- Nick

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:
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):


Great stuff - thanks very much for all your work! A few comments (with
questions without any comments snipped).
- 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.


I'm going to try to steer clear of using "default" for parameters
unless I'm talking about the constructor supplied by default if you
don't give one (in a reference type). I believe "parameterless" is
actually the correct term - for instance, you can't supply a default
constructor for a reference type, you just get given one if you don't
supply any constructors at all. This is one piece of terminology which
is much abused (including by Microsoft), but I believe the difference
can be useful, so I'll try to draw a distinction.

On the other hand, I've just looked at the CLR spec and that mentions
default constructors 3 times, so maybe I'm just wrong. Shame though, I
thought it was quite handy...
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.


Explaining here that CLR types *can* have parameterless constructors,
but C# can't generate such types would be a good idea. It took me a
while to work out what was meant here.
- 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.


I would personally stick to short in the example here, just so people
don't think it's got *anything* to do with being unsigned!
- 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.


It might be worth making the question a bit more explicit in terms of
what a global hook is, just for clarity. (Or put it in the answer.)
- 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;


Add
1d // a System.Double floating-point number
1m // a System.Decimal floating-point number

Then at least even if there aren't all the variants in terms of case,
all the bases are covered, as it were.
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...ry/en-us/csspe
c/html/vclrfcsharpspec_C.asp


Unless anyone particularly minds, I'd like to keep to the ECMA spec
rather than the MS spec for references. This may be impossible for 2.0
features for a while, but the FAQ can be fixed later for those features
:)
- 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


Gosh, I'm surprised I don't have that in there already. I'll include a
link to my page about parameter passing...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
Nick Malik <ni*******@hotmail.nospam.com> wrote:
I wasn't aware of your effort.

Have you thought of allowing your FAQs to be compiled on a Wiki server?

In my mind, the advantage would be that the FAQs can be honed by the
community (or the MVP) on an ongoing basis, not just a one-off effort.

Either way, it's a valuable effort. Thanks for doing this work.


I considered using a Wiki, but in my experience they can end up being
hard to read due to conversation, and harder to keep consistent.

As it is, I'm suggesting that discussion about FAQ answers should occur
on the newsgroup, and I'll keep it up to date with that (once
conversation has settled down on any particular point). I'm also
currently adding extra questions/answers autonomously occasionally,
which will hopefully stop in a month or so.

It's certainly not going to be a one-off effort - merely a more
co-ordinated one than might happen with a Wiki :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ui****************@TK2MSFTNGP10.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:
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): <snip>
- 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.
It might be worth making the question a bit more explicit in terms of
what a global hook is, just for clarity. (Or put it in the answer.)


Perhaps a more general "Why doesn't C# allow exporting functions in dlls",
especially considering that it is possible in IL(If memory serves, don't
recall how to do it). I can find the answer, just don't want to dig
through Brumme's blogs right now.


Found the blog I was referencing[1].

The basic reasons are how such calls enter the assembly, specifically which
app domain. The current mechanisms are not paticularly desirable and may be
confusing at best.

1. http://blogs.msdn.com/cbrumme/archiv.../15/51325.aspx
Anyway, after explaining why you can't export functions, it would be easy
to explain why xprocs, global hooks, and other such things aren't possible
with C#.
- 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;


Add
1d // a System.Double floating-point number
1m // a System.Decimal floating-point number

Then at least even if there aren't all the variants in terms of case,
all the bases are covered, as it were.
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...ry/en-us/csspe
c/html/vclrfcsharpspec_C.asp


Unless anyone particularly minds, I'd like to keep to the ECMA spec
rather than the MS spec for references. This may be impossible for 2.0
features for a while, but the FAQ can be fixed later for those features
:)


I agree on this, ECMA spec is prefereable. ALthough,is the MS spec just a
clone or is it a rewrite?(I've never actually used the msdn spec). - 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


Gosh, I'm surprised I don't have that in there already. I'll include a
link to my page about parameter passing...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #9
Richard,
Otherwise be happy to I'll give it a try.


Go ahead. I'll have a look when it's done and let you know if I
disagree with something :-)

Mattias

--
Mattias Sjögren [MVP]
Nov 15 '05 #10
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:

<snip>

I've now added these questions (and answers):
- what's the difference between using cast syntax and the 'as' operator?
- how do I use an alias for a namespace or class?
- why doesn't C# have checked exceptions?
- why are struct constructors in C# required to have at least one argument?
- how can I show an int as a binary number - a string of 1's and 0's
- why doesn't calling Initialize() on a reference-type array fill in the
array with objects?
- How do I tell C# what kind of literal number I want? (f, L, U etc) (C# as
- What's the difference between the ref and out modifiers on parameters?
I haven't added these questions, as I believe more detailed answers are
pending:
- why does C# return an int when you add two bytes/shorts/sbytes/ushorts
together?
- how can I speed up P/Invoke calls made through C# to external functions?
- How can I implement a *global* hook in C#?


--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
While this is a great first step, I would like to see something where this
has teeth - in other words when that "do you need the framework installed to
run that .NET app" comes up for the dozen time that day, that we all have a
consistent approach in directing people to the FAQ.

--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
..NET Blog http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/de...tml/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
---------------------------------------------------------------
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:

<snip>

I've now added these questions (and answers):
- what's the difference between using cast syntax and the 'as' operator?
- how do I use an alias for a namespace or class?
- why doesn't C# have checked exceptions?
- why are struct constructors in C# required to have at least one argument? - how can I show an int as a binary number - a string of 1's and 0's
- why doesn't calling Initialize() on a reference-type array fill in the
array with objects?
- How do I tell C# what kind of literal number I want? (f, L, U etc) (C# as - What's the difference between the ref and out modifiers on parameters?


I haven't added these questions, as I believe more detailed answers are
pending:
- why does C# return an int when you add two bytes/shorts/sbytes/ushorts
together?
- how can I speed up P/Invoke calls made through C# to external functions? - How can I implement a *global* hook in C#?


--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #12
"Sam Gentile [MVP - C#/.NET]" <sa*@nomail.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
While this is a great first step, I would like to see something where this
has teeth - in other words when that "do you need the framework installed to run that .NET app" comes up for the dozen time that day, that we all have a consistent approach in directing people to the FAQ.

Hi Sam,

I totally agree. The way Jon has published it we can drill down on a FAQ
and easily post the URL to the exact answer.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #13
Yes, also for the
- why are struct constructors in C# required to have at least one argument?
but I'll just submit that for an update.

Sorry for the delay - it's been a hectic couple of weeks...
Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:

<snip>

I've now added these questions (and answers):
- what's the difference between using cast syntax and the 'as' operator?
- how do I use an alias for a namespace or class?
- why doesn't C# have checked exceptions?
- why are struct constructors in C# required to have at least one

argument? - how can I show an int as a binary number - a string of 1's and 0's
- why doesn't calling Initialize() on a reference-type array fill in the
array with objects?
- How do I tell C# what kind of literal number I want? (f, L, U etc) (C# as - What's the difference between the ref and out modifiers on parameters?


I haven't added these questions, as I believe more detailed answers are
pending:
- why does C# return an int when you add two bytes/shorts/sbytes/ushorts
together?
- how can I speed up P/Invoke calls made through C# to external functions? - How can I implement a *global* hook in C#?


--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #14
Richard A. Lowe <ch*****@yumspamyumYahoo.com> wrote:
Yes, also for the
- why are struct constructors in C# required to have at least one

argument?
but I'll just submit that for an update.

Sorry for the delay - it's been a hectic couple of weeks...


No rush - after all it took me about 6 months to get the first version
out of the door!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #15
Joe Mayo [C# MVP] <jm***@nospamAtCSharpDashStation.com> wrote:
"Sam Gentile [MVP - C#/.NET]" <sa*@nomail.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
While this is a great first step, I would like to see something where this
has teeth - in other words when that "do you need the framework installed
to run that .NET app" comes up for the dozen time that day, that we all have
a consistent approach in directing people to the FAQ.

I'd love to see that too - but it's certainly *starting*. Every so
often I'll see a question and be just ready to post a FAQ answer, only
to see that someone's got there already :)
I totally agree. The way Jon has published it we can drill down on a FAQ
and easily post the URL to the exact answer.


Yes - although I have concerns about the way I've done it, now. The FAQ
is already getting pretty large, and I can only see it getting larger.
I wonder whether I shouldn't have split it into more pages to start
with, or given some what of posting a URL which would redirect to the
appropriate place in a way which gives more flexibility.

I'm still thinking on that (a simple servlet which knew how to redirect
appropriately would do, along with some easy way of getting the right
URL to post) but I'd welcome any ideas - including "it's fine the way
it is".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #16

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
I totally agree. The way Jon has published it we can drill down on a FAQ and easily post the URL to the exact answer.


Yes - although I have concerns about the way I've done it, now. The FAQ
is already getting pretty large, and I can only see it getting larger.
I wonder whether I shouldn't have split it into more pages to start
with, or given some what of posting a URL which would redirect to the
appropriate place in a way which gives more flexibility.

I'm still thinking on that (a simple servlet which knew how to redirect
appropriately would do, along with some easy way of getting the right
URL to post) but I'd welcome any ideas - including "it's fine the way
it is".

I like the way the Windows Forms FAQ on SyncFusion is done. It is really
easy to find things, navigate, and reference.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #17
Joe Mayo [C# MVP] <jm***@nospamAtCSharpDashStation.com> wrote:
I'm still thinking on that (a simple servlet which knew how to redirect
appropriately would do, along with some easy way of getting the right
URL to post) but I'd welcome any ideas - including "it's fine the way
it is".


I like the way the Windows Forms FAQ on SyncFusion is done. It is really
easy to find things, navigate, and reference.


Right. (The first page is still huge for that, but only because there
are *so* many questions.) I'll look at the options for changing the
format. The downside is that existing references will become invalid,
but I could have a transitional period...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matt W | last post by:
Hi all, I'm planning to use MySQL's full-text search for my forum system (possibly 5+ million posts). I've been playing with it a lot lately to see the performance and functionality and have...
10
by: Ron Ruble | last post by:
I'd like to get suggestions from some of the folks here regarding tools and processes for a new, small development team. I'm starting a new job next week, and part of the fun is being able to...
1
by: Brian Basquille | last post by:
Hello all. Have been working on the Air Hockey game on and off for a couple of weeks now.. but have had plenty of other assignments to keep me busy along with it. But i would like some...
62
by: A.M. Kuchling | last post by:
Here are some thoughts on reorganizing Python's documentation, with one big suggestion. The tutorial seems to be in pretty good shape because Raymond Hettinger has been keeping it up to date. ...
6
by: iclinux | last post by:
I have to build a GUI applicaiton that could run on different OS such as windows and *nix, which GUI toolkit is better? Best Regards.
45
by: Gregory Petrosyan | last post by:
1) From 2.4.2 documentation: There are two new valid (semantic) forms for the raise statement: raise Class, instance raise instance 2) In python: >>> raise NameError Traceback (most recent...
26
by: Frank Samuelson | last post by:
I love Python, and it is one of my 2 favorite languages. I would suggest that Python steal some aspects of the S language. ------------------------------------------------------- 1. Currently...
0
rnd me
by: rnd me | last post by:
Purpose: Allows you to create "presets" for text form inputs. "Lightweight and simple to setup, it adds a lot of convenience for ~1kb of code." Only one function, two parameters: First...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.