常见问题建议 [英] FAQ Suggestions

查看:74
本文介绍了常见问题建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我不久前提出的一些问题的答案

for m.p.d.l.c常见问题Jon Skeet正在组织。这是Jon的意图

这些在被包括之前被小组磨练 - 所以磨练掉。这是我的

意图,你只是不会因为我的

答案中的任何不准确而撕毁我一个新的。我赞扬Anders Hejlsberg在这个帖子中回答了我关于

非int运算符的问题(这是以下我的答案的基础

):
http://www.gotdotnet。 com / Community / M ... aspx?id = 157575

- 使用强制语法和'as''运算符之间的区别是什么? (C#

作为一种语言)


使用'as''运算符与C#中的强制转换器有三种不同的方式:

1.当您尝试转换的变量不是

请求的类型或其继承链

时,它返回null。只能应用于转换为

引用类型的引用类型变量。

3.使用''as''不会执行用户定义的转换,例如隐式或

显式转换运算符,它的转换语法是什么。


实际上在IL中定义了两个完全不同的操作

handle这两个关键字(castclass和isinst指令) - 它不仅仅是'#b $ b'由C#编写的语法糖来获得这种不同的行为。与演员相比,

''作为''运算符在微软的v1.0和v1.1中的运行速度稍微快了一些(即使在情况下没有无效的演员阵容

会因为例外而严重降低演员阵容的表现。


- 我如何使用命名空间的别名或类? (C#作为一种语言)


使用''using''指令为长命名空间或类创建别名。

然后你可以使用它您通常会使用该类或

名称空间的任何地方。 using alias在你声明它的命名空间中有一个范围

in。

//命名空间:

使用act = System.Runtime.Remoting .Activation;

//班级

使用list = System.Collections.ArrayList;


- 为什么不是C#检查过例外? (C#作为一种语言??)


在某些圈子中,已检查的异常是一个非常激烈争论的主题,

特别是对于有经验的Java开发人员来说,或者另外

学习,C#。以下是一些深入讨论该问题的资源:
http:/ /www.artima.com/intv/handcuffs.html
http://www.mindview.net/Etc/Discussi...netExceptions


- 为什么C#中的结构构造函数需要至少有一个参数?

(C#作为一种语言)


.NET运行时不能保证无参数构造函数将被调用
。如果结构允许默认的无参数构造函数,那么它将暗示这些默认构造函数将始终*被调用。

但是,运行时不能保证这一点。例如,一个

值类型的数组将被初始化为它的成员的初始值(即数字类型原始成员的
0,参考类型的null等等) )*不要

默认构造函数*中提供的值 - 这使得结构更好

通过不必调用构造函数代码来执行。在构造函数中强制执行至少
一个参数可以减少某人定义一个构造函数的可能性,然后他们希望每次调用一个

构造它们的结构类型。


- 如何将int显示为二进制数字 - 一个1'和0'的字符串


转换类有一个静态ToString()方法的重载,它带有两个整数的
并返回一个填充了指定数字的字符串

base。

Convert.ToString(128,2);


- 当你添加两个字节/短片/ sbytes / ushorts时,为什么C#返回一个int

在一起?


在执行

算术运算之前,C#总是将8位和16位值提升为32位。实际上,在32位处理器上模拟8或16位
算术的效率要低于在32位/ b $ b位中进行操作的效率。


C#缩小转换次数(可能会影响

数量的转换,如将int转换为short)总是明确的。相比之下,他们可以在C / C ++中隐式地发生
。这就是为什么你必须把像ushort + ushort这样的

表达式的结果转回到作业中的ushort。


- 我怎样才能加速P / Invoke通过C#调用外部函数?

一种简单的方法是将SuppressUnmanagedCodeSecurityAttribute应用于你正在制作的

方法,类或接口平台调用

通过。应用此属性会绕过运行时安全检查

确保调用外部方法的代码具有UnmanagedCode

权限。这可能会导致您的应用程序严重违反安全规定

并使用它意味着保护此非托管呼叫是开发人员的责任,而不是框架。


例如:

[System.Security.SuppressUnmanagedCodeSecurityAttri bute()]

[DllImport(" User32.dll")]

public extern static int GetCursorPos(ref Point thePoint);


- 为什么不在引用类型数组上调用Initialize()填充

包含对象的数组?


System.Array类实例方法Initialize()仅用于
将值类型数组初始化为其默认值并且在

引用类型数组上无效。 (实际上它甚至不适用于C#值类型

结构,因为这些结构可以没有默认构造函数用于初始化到

调用。)


- 如何在C#中实现* global *钩子?


这是不可能的,因为C#/ .NET不支持基本元素:

a DLL导出提供一致的函数指针。请参阅此处
http://support.microsoft.com/?kbid= 318804 在底部再多一点

详细信息。


- 我如何告诉C#我想要什么样的字面数字? (f,L,U等)(C#as


如果您需要告诉C#您希望它将文字视为特定的

类型数字,您可以通过在您提供的文字的末尾添加数字类型后缀来实现:


1U; //一个未标记的int

1ul; //一个unsigned long

1f; //一个System.Single浮点数;


这个有点重要因为有时候你必须将文字与某些东西的

签名相匹配,或者指定值'击败''你不喜欢的隐性演员

行为。例如:

Hashtable names = new Hashtable(100,0.1);

不会编译因为它需要签名(int,float)而上面是

(int,double)。该行应为:

Hashtable names = new Hashtable(100,0.1f);


完整的后缀列表位于C.1.8文字下c#

规范的语法部分。
http://msdn.microsoft.com/library/de...harpspec_C.asp


- ref和out修饰符之间的区别是什么参数?


ref和out方法参数都应用于方法的参数

,两者都意味着参数将通过引用传递; (或者通过引用获得
值类型变量或通过引用引用类型变量)。

但是,out参数允许您传入未初始化的变量

就像这样,并保证它会以它的值设置回来(只要

调用方法是用C#编写的,无论如何)。

int i;

DoStuffByRef(out i);

//我现在是一个可用的int值


-

C#,.NET和复杂自适应系统:
http://blogs.geekdojo.net/Richard

解决方案

Richard A. Lowe< ch ***** @ yumspamyumYahoo.com>写道:

这里是我不久前建议的一些问题的答案
m.p.d.l.c常见问题Jon Skeet正在组织。乔恩的意图是,在被包括之前,这些被组织磨练 - 所以磨练掉。我的
意图是你不会因为我的答案中的任何不准确而撕毁我一个新的。我赞扬Anders Hejlsberg在这篇帖子中回答了我关于非int运算符的问题(这是我下面的答案的基础):


很棒的东西 - 非常感谢您的所有工作!一些评论(

问题没有任何评论被剪断)。

- 为什么C#中的结构构造函数需要至少有一个参数?
(C#as一种语言)

.NET运行时不能保证将调用无参数构造函数。如果结构允许默认的无参数构造函数,它将暗示这些默认构造函数将*始终*被调用。


我将试图避免使用默认对于参数

,除非我在谈论默认情况下提供的构造函数,如果你是
不给一个(参考类型)。我相信无参数的

实际上是正确的术语 - 例如,你不能为参考类型提供一个默认的

构造函数,如果你不是,你就得到一个

提供任何构造函数。这是一个术语,其中b $ b被滥用(包括微软),但我相信差异

可能很有用,所以我会尝试绘制一个区别。


另一方面,我刚刚查看了CLR规范并且提到了

默认构造函数3次,所以也许我''我错了。很遗憾,我认为它是非常方便的......

然而,运行时不能保证这一点。例如,一个
值类型的数组将被初始化为它的成员的初始值(即数字类型原始成员为0,参考类型为null等)*不对
默认构造函数*中提供的值 - 通过不必调用构造函数代码使结构更好地执行。在构造函数中强制执行至少一个参数可以减少某人定义构造函数的可能性,这些构造函数在每次构造其结构类型之一时都会被调用。


这里解释CLR类型*可以*具有无参数构造函数,

但是C#不能生成这样的类型将是一个好主意。我花了一个

来计算这里的含义。

- 当你添加两个字节/短裤/ sbytes / ushorts时,为什么C#会返回一个int 在一起?

在执行算术运算之前,C#总是将8位和16位值提升为32位。实际上,在32位处理器上模拟8或16位算术的效率要低于在32位处执行操作的效率。

C#缩小转换次数(转换次数)这可能会影响数字的大小,如将int转换为short)总是明确的。相比之下,它们可以在C / C ++中隐式发生。这就是为什么你必须将像ushort + ushort这样的表达式的结果强制转换为赋值中的ushort。


我个人会坚持在这里的例子,只是这样的人

不要认为它有*任何*与没有签名!

- 如何在C#中实现* global * hook?

这是不可能的,因为C#/ .NET不支持基本元素: http://support.microsoft.com/?kbid=在底部318804 以获得更多细节。


为了清晰起见,可能值得将这个问题更明确地说明是什么? (或者把它放在答案中。)

- 我怎么告诉C#我想要什么样的字面数字? (f,L,U等)(C#as

如果您需要告诉C#您希望它将文字视为特定的数字类型,您可以通过添加
你提供的文字结尾的数字类型后缀:

1U; //一个unsiged int
1ul; //一个unsigned long
1f; / / a.System.Single浮点数;


添加

1d //一个System.Double浮点数

1m //一个System.Decimal浮点数


然后至少即使没有所有变种的情况,

all这些基础都被覆盖了。

这有点重要,因为有时候你必须将文字与某事物的
签名相匹配,或者指定值'击败''隐式演员表
你不喜欢的行为。例如:
Hashtable names = new Hashtable(100,0.1);
不会编译,因为它需要签名(int,float)以上是
(int,double)该行应为:
Hashtable names = new Hashtable(100,0.1f);

后缀的完整列表位于c#
规范的语法部分中C.1.8文字。
http://msdn.microsoft.com/library/de...ry/en-us/csspe
c / html / vclrfcsharpspec_C.asp


除非有人特别介意,否则我想遵守ECMA规范

而不是MS规范参考。对于2.0

功能暂时可能是不可能的,但常见问题解答可以在以后修复这些功能

:)

- 什么'参数的ref和out修饰符之间的区别是什么?

ref和out方法参数都应用于方法的参数
并且两者都意味着参数将被传递通过引用 (通过引用的值类型变量或引用的引用类型变量)。
out参数允许您传入未初始化的变量
,并保证它会来返回它的值设置(只要
被调用的方法是用C#编写的,无论如何)。

int i;
DoStuffByRef(out i);
//我现在是一个可用的int值


天哪,我很惊讶我已经没有了。我将在我的页面中包含一个关于参数传递的

链接...


-

Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget

如果回复小组,请不要给我发邮件




Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

Richard A. Lowe< ch ***** @ yumspamyumYahoo.com>写道:

这里有一些问题的答案我建议很短的时间
前面的m.p.d.l.c常见问题Jon Skeet正在组织。这是乔恩的意图
这些被小组磨练之后才被包括在内 - 所以要磨练掉。我的
意图是你不会因为我的
答案中的任何不准确而撕毁我一个新的。我赞扬Anders Hejlsberg回答我的问题
关于
非线性运算符不久之前在这个帖子中(这是我以下答案的
基础):
< snip>

- 如何在C#中实现* global * hook?

这是不可能的,因为C#/ .NET不支持必要的
元素:
DLL导出提供一致的函数指针。请参阅此处
http://support.microsoft.com/?kbid=底部的318804 可以提供更多详细信息。
为了清晰起见,可能值得将问题更明确地说明一个全球性的问题。 (或者把它放在答案中。)




也许更通用的为什么C#不允许在dll中导出函数,

特别考虑到它可能在IL中(如果记忆服务,不要
回忆如何做)。我可以找到答案,只是不想深入了解

Brumme的博客。


无论如何,在解释为什么你可以没有导出函数,很容易解释为什么xprocs,全局挂钩和其他类似的东西是不可能的。

C#。

- 如何告诉C#我想要什么样的字面数? (f,L,U等)(C#


如果您需要告诉C#您希望它将文字视为
特定类型的你可以通过在你提供的文字的最后添加一个数字类型后缀来实现这个数字:

1U; //一个未命名的int
1ul; //一个unsigned long
1f; //一个System.Single浮点数;



添加
1d //一个System.Double浮点数
1m //一个System.Decimal浮点数

然后至少即使没有所有的变种,也包括所有的基础原样。

这有点重要,因为有时你必须将文字与
某些东西的签名相匹配,或者指定值为''失败' 你不喜欢的隐式演员行为。例如:
Hashtable names = new Hashtable(100,0.1);
不会编译,因为它需要签名( int,float)以上
(int,double)。该行应为:
Hashtable names = new Hashtable(100,0.1f);

后缀的完整列表位于C#
规范的C语法部分中.1.8文字。
http://msdn.microsoft.com/library/de...ry/en-us/csspe
c / html / vclrfcsharpspec_C.asp


除非有人特别介意,否则我想遵守ECMA规范
而不是MS规范的参考。对于2.0
功能有一段时间这可能是不可能的,但是FAQ可以在以后针对这些功能进行修复
:)




我同意这一点,ECMA规范是可取的。虽然,MS规范只是一个

克隆还是重写?(我从未真正使用过msdn规范)。 - 参数的ref和out修饰符之间有什么区别?

ref和out方法参数都应用于
方法的参数
两者都意味着该参数将通过引用传递。 (通过引用的值类型变量或通过
引用的引用类型变量)。
然后,out参数允许您传入未初始化的
变量喜欢这样,并保证它会以它的值设置回来(只要
被称为方法是用C#编写的,无论如何)。

int i;
DoStuffByRef(out i);
//我现在是一个可用的int值

天哪,我很惊讶我没有已经。我会在我的页面中添加一个关于参数传递的链接......

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



Richard,

- 为什么C#当你一起添加两个字节/短片/字节/ ushorts时返回一个int?

在执行算术运算之前,C#总是将8位和16位值提升为32位。实际上,在32位处理器上模拟8或16位算术的效率要低于在32位处执行操作的效率。

C#缩小转换次数(转换次数)这可能会影响数字的大小,如将int转换为short)总是明确的。相比之下,它们可以在C / C ++中隐式发生。这就是为什么你必须将像ushort + ushort这样的表达式的结果强制转换为赋值中的ushort。



这里我认为值得指出复合

赋值运算符(14.13.2)的特殊情况。


- 如何通过C#加速对外部函数的P / Invoke调用?

一种简单的方法是将SuppressUnmanagedCodeSecurityAttribute应用于
方法,您正在进行平台调用的类或接口调用
。应用此属性会绕过运行时安全检查,以确保调用外部方法的代码具有UnmanagedCode
权限。这可能会导致您的应用程序严重违反安全规定
并使用它意味着保护此非托管调用是开发人员的责任,而不是框架。


好​​吧SUCSA只是加速互操作代码的一种方式(可能是最后一个我建议人们使用的
,因为安全性我认为促进正确使用进/出

属性和参数输入更重要,以避免不必要的编组,并且

制作矮胖而不是聊天而不是聊天如果可能,请调用。


- 如何在C#中实现* global * hook?
这是不可能的,因为C#/ .NET不支持基本元素:
DLL导出提供一致的函数指针。




C#不支持它,但C ++和IL Assembler确实支持它。但是获得

函数导出只是问题的一部分。强制将CLR加载到钩子代码的每个进程中都是一个可怕的想法

无论如何都要运行。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


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

解决方案

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



"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



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.


这篇关于常见问题建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆