在一起?除非有人特别介意,否则我想遵守ECMA规范喜欢这样,并保证它会以它的值设置回来(只要被称为方法是用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 agofor the m.p.d.l.c FAQ Jon Skeet is organizing. It was Jon''s intention thatthese be ''honed'' by the group before being included - so hone away. It''s myintention that you merely not ''tear me a new one'' for any inaccuracies in myanswers. I give credit to Anders Hejlsberg for answering my question aboutthe non-int operators a short while ago in this thread (which is the basisfor 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 ofthe requested type or in it''s inheritance chain2. It can only be applied to reference type variables converting toreference types.3. Using ''as'' will not perform user-defined conversions, such as implicit orexplicit conversion operators, which casting syntax will do.There are in fact two completely different operations defined in IL thathandle these two keywords (the castclass and isinst instructions) - it''s notjust ''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''sCLR compared to casting (even in cases where there are no invalid castswhich 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 ornamespace. The using alias has a scope within the namespace you declare itin.// namespace:using act = System.Runtime.Remoting.Activation;// classusing 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 additionallylearning, 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 becalled. If structs where to allow default, parameterless constructors, itwould imply that these default constructors would *always* be called.However, the runtime can not make this guarantee. For example an array ofvalue 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 tothe values provided in a default constructor* - which makes structs betterperforming by not having to call constructor code. Enforcing a minimum ofone parameter in the constructor reduces the possibility that someone willdefine a constructor that they then expect to be called every time one oftheir struct types is constructed.- how can I show an int as a binary number - a string of 1''s and 0''sThe convert class has an overload of the static ToString() method that takestwo ints and returns a string populated with the number in the specifiedbase.Convert.ToString(128, 2);- why does C# return an int when you add two bytes/shorts/sbytes/ushortstogether?C# always promotes 8 and 16 bit values to 32 bits before performingarithmetic 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 ofexpressions 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 themethod, class or interface that you are making platform invoke callsthrough. Apply this attribute bypasses the runtime security check thatensures that the code calling the external method has UnmanagedCodepermission. 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.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 thearray with objects?The System.Array class instance method Initialize() exists solely toinitialize value type arrays to their default values and is not valid onreference type arrays. (In fact it is not even intended for C# value typestructs, as these structs can have no default constructor for Initialize tocall.)- 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 moredetail.- How do I tell C# what kind of literal number I want? (f, L, U etc) (C# asIf you need to tell C# that you want it to treat a literal as a particulartype of number, you may do so by adding a number type suffix at the end ofthe literal you provide:1U; // an unsiged int1ul; // an unsigned long1f; // a System.Single floating-point number;This is somewhat important because sometimes you must match a literal to thesignature of something or specify the value to ''defeat'' an implicit castbehavior 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 methodand both mean that the argument will be passed "by reference" (either avalue type variable by reference or a reference type variable by reference).The out parameter, however, allows you to pass in an uninitialized variablelike so and guarantees it will come back with it''s value set (so long as thecalled 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 (withquestions 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 parametersunless I''m talking about the constructor supplied by default if youdon''t give one (in a reference type). I believe "parameterless" isactually the correct term - for instance, you can''t supply a defaultconstructor for a reference type, you just get given one if you don''tsupply any constructors at all. This is one piece of terminology whichis much abused (including by Microsoft), but I believe the differencecan be useful, so I''ll try to draw a distinction.On the other hand, I''ve just looked at the CLR spec and that mentionsdefault constructors 3 times, so maybe I''m just wrong. Shame though, Ithought 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 awhile 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 peopledon''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 ofwhat 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;Add1d // a System.Double floating-point number1m // a System.Decimal floating-point numberThen 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.aspUnless anyone particularly minds, I''d like to keep to the ECMA specrather than the MS spec for references. This may be impossible for 2.0features 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 valueGosh, I''m surprised I don''t have that in there already. I''ll include alink to my page about parameter passing...--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in messagenews: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''trecall how to do it). I can find the answer, just don''t want to dig throughBrumme''s blogs right now.Anyway, after explaining why you can''t export functions, it would be easy toexplain why xprocs, global hooks, and other such things aren''t possible withC#. - 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 aclone 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 tooRichard, - why does C# return an int when you add two bytes/shorts/sbytes/ushortstogether?C# always promotes 8 and 16 bit values to 32 bits before performingarithmetic 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 ofexpressions like ushort + ushort back to ushort in an assignment.Here I think it''s worth pointing out the special case for compoundassignment 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 themethod, class or interface that you are making platform invoke callsthrough. Apply this attribute bypasses the runtime security check thatensures that the code calling the external method has UnmanagedCodepermission. 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 thelast one I''d recommend people to use, because of the security stuff).I think it''s more important to promote correct use of In/Outattributes and parameter typing to avoid unnecessary marshaling, andmaking "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 afunction export is only part of the problem. It would be a terribleidea to force the CLR to be loaded into every process the hook code isrun in anyway.Mattias--Mattias Sj?gren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.comPlease reply only to the newsgroup. 这篇关于常见问题建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-08 18:55