@丹帕默为什么在MyCommon.targets上加一个cpp外壳是错误的?CPP能做的只是一些让语言变得异常困难的事情。在IMO中,更糟糕的是,在编写throw new ArgumentNullException("argumentName");时,开发人员必须手动将参数名串接起来。代码契约应该可以解决这一问题,但它需要一个使用.targets的固定的IL重写器,这对于固定CPP调用来说应该不会更糟。
usingstaticSystem.Console;// Note the static keyword
namespace CoolCSharp6Features { publicclass Program { publicstaticint Main(string[] args) {
WriteLine("Hellow World without Console class name prefix!");
/// <summary> /// Prints a message when in debug mode /// </summary> [MethodImpl(MethodImplOptions.AggressiveInlining)] publicstaticunsafevoid Log(object message){ #if DEBUG
Console.WriteLine(message); #endif }
/// <summary> /// Prints a formatted message when in debug mode /// </summary> /// <param name="format">A composite format string</param> /// <param name="args">An array of objects to write using format</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] publicstaticunsafevoid Log(string format, paramsobject[] args){ #if DEBUG
Console.WriteLine(format, args); #endif }
/// <summary> /// Computes the square of a number /// </summary> /// <param name="x">The value</param> /// <returns>x * x</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] publicstaticdouble Square(double x){ return x * x; }
/// <summary> /// Wipes a region of memory /// </summary> /// <param name="buffer">The buffer</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] publicstaticunsafevoid ClearBuffer(refbyte[] buffer){
ClearBuffer(ref buffer, 0, buffer.Length); }
/// <summary> /// Wipes a region of memory /// </summary> /// <param name="buffer">The buffer</param> /// <param name="offset">Start index</param> /// <param name="length">Number of bytes to clear</param> [MethodImpl(MethodImplOptions.AggressiveInlining)] publicstaticunsafevoid ClearBuffer(refbyte[] buffer, int offset, int length){ fixed(byte* ptrBuffer =&buffer[offset]){ for(int i =0; i < length;++i){ *(ptrBuffer + i)=0; } } }
publicstaticclass WriteToConsoleExtension { // Extension to all types publicstaticvoid WriteToConsole(thisobject instance, string format, paramsobject[] data) {
Console.WriteLine(format, data); } }
class Program { staticvoid Main(string[] args) {
Program p =new Program(); // Usage of extension
p.WriteToConsole("Test {0}, {1}", DateTime.Now, 1); } }