必赢的网址登录 > 必赢网址 > 必赢的网址登录Top 15 不起眼却有大作用的 .NET功

原标题:必赢的网址登录Top 15 不起眼却有大作用的 .NET功

浏览次数:160 时间:2019-09-25

必赢的网址登录 1

目录

  1. ObsoleteAttribute
  2. 设置默许值属性: DefaultValueAttribute
  3. DebuggerBrowsableAttribute
  4. ??运算符
  5. Curry 及 Partial 方法
  6. WeakReference
  7. Lazy
  8. BigInteger
  9. 非官方入眼字:__arglist __reftype __makeref __refvalue
  10. Environment.NewLine
  11. ExceptionDispatchInfo
  12. Environment.FailFast
  13. Debug.Assert, Debug.WriteIf 及 Debug.Indent
  14. Parallel.For 及 Parallel.Foreach
  15. IsInfinity方法

正文介绍了.Net 常被忽视,但却百般有客商的17个职能,包涵ObsoleteAttribute,DefaultValueAttribute,DebuggerBrowsableAttribute,?运算符,Curry及 Partial 方法…

1. ObsoleteAttribute

ObsoleteAttribute: 可适用于除程序集、模块、参数或重临值以外的保有程序成分。将成分标记为 Obsolete 可达成公告顾客的法力,注脚该因素在成品以往的新本子中会被移除。

Message属性包含三个字符串,可收获变通方法消息,是对可选程序成分的表达。

IsError属性:是Bool 类型,如若将值设为“True”,则编写翻译器会将选拔已不符合时机的次第元素正是错误。

   1:  public static class ObsoleteExample

   2:  {

   3:      // Mark OrderDetailTotal As Obsolete.

   4:      [ObsoleteAttribute("This property (DepricatedOrderDetailTotal) is obsolete. Use InvoiceTotal instead.", false)]

   5:      public static decimal OrderDetailTotal

   6:      {

   7:          get

   8:          {

   9:              return 12m;

  10:          }

  11:      }

  12:  

  13:      public static decimal InvoiceTotal

  14:      {

  15:          get

  16:          {

  17:              return 25m;

  18:          }

  19:      }

  20:  

  21:      // Mark CalculateOrderDetailTotal As Obsolete.

  22:      [ObsoleteAttribute("This method is obsolete. Call CalculateInvoiceTotal instead.", true)]

  23:      public static decimal CalculateOrderDetailTotal()

  24:      {

  25:          return 0m;

  26:      }

  27:  

  28:      public static decimal CalculateInvoiceTotal()

  29:      {

  30:          return 1m;

  31:      }

  32:  }

如果运行程序,编译器就会报错以及Warning。

   1:  Console.WriteLine(ObsoleteExample.OrderDetailTotal);

   2:  Console.WriteLine();

   3:  Console.WriteLine(ObsoleteExample.CalculateOrderDetailTotal;

必赢的网址登录 2

2. 通过“DefaultValueAttribute”属性可安装缺省值

DefaultValueAttribute 钦赐属性的默许值。能够应用另外值创制DefaultValueAttribute。成员的暗中认可值平日是其开首值。可视化设计器能够利用暗许值重新载入参数成员的值。代码生成器也可使用默许值鲜明是不是为成员变动代码,可是必得在成员函数中装置开始值。

   1:  public class DefaultValueAttributeTest

   2:  {

   3:      public DefaultValueAttributeTest()

   4:      {

   5:          // Use the DefaultValue propety of each property to actually set it, via reflection.

   6:          foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this))

   7:          {

   8:              DefaultValueAttribute attr = (DefaultValueAttribute)prop.Attributes[typeof(DefaultValueAttribute)];

   9:              if (attr != null)

  10:              {

  11:                  prop.SetValue(this, attr.Value);

  12:              }

  13:          }

  14:      }

  15:  

  16:      [DefaultValue]

  17:      public int Age { get; set; }

  18:  

  19:      [DefaultValue("Anton")]

  20:      public string FirstName { get; set; }

  21:  

  22:      [DefaultValue("Angelov")]

  23:      public string LastName { get; set; }

  24:  

  25:      public override string ToString()

  26:      {

  27:          return string.Format("{0} {1} is {2}.", this.FirstName, this.LastName, this.Age);

  28:      }

  29:  }

3. DebuggerBrowsableAttribute

代表成员是还是不是在Debugger 变量窗口体现以及哪些展示。

   1:  public static class DebuggerBrowsableTest

   2:  {

   3:      private static string squirrelFirstNameName;

   4:      private static string squirrelLastNameName;

   5:  

   6:      // The following DebuggerBrowsableAttribute prevents the property following it 

   7:      // from appearing in the debug window for the class.

   8:      [DebuggerBrowsable(DebuggerBrowsableState.Never)]

   9:      public static string SquirrelFirstNameName 

  10:      {

  11:          get

  12:          {

  13:              return squirrelFirstNameName;

  14:          }

  15:          set

  16:          {

  17:              squirrelFirstNameName = value;

  18:          }

  19:      }

  20:  

  21:      [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]

  22:      public static string SquirrelLastNameName

  23:      {

  24:          get

  25:          {

  26:              return squirrelLastNameName;

  27:          }

  28:          set

  29:          {

  30:              squirrelLastNameName = value;

  31:          }

  32:      }

  33:  }

渐渐调试程序时,就能够意识代码会一贯在执行:

   1:  DebuggerBrowsableTest.SquirrelFirstNameName = "Hammy";

   2:  DebuggerBrowsableTest.SquirrelLastNameName = "Ammy";

4. ??操作符

?? 运算符称作 null 合併运算符。假诺此运算符的左操作数不为 null,则此运算符将重回左操作数;不然重回右操作数,假诺在品味将得感到null 值的品类分配给不得以为null值的类型时,未有动用??运算符,则会生成编写翻译时的一无所能。如若应用强制转变,且当前未定义可认为null 值的品种,则会抓住 InvalidOperationException 相当。

   1:  int? x = null;

   2:  int y = x ?? -1;

   3:  Console.WriteLine("y now equals -1 because x was null => {0}", y);

   4:  int i = DefaultValueOperatorTest.GetNullableInt() ?? default(int);

   5:  Console.WriteLine("i equals now 0 because GetNullableInt() returned null => {0}", i);

   6:  string s = DefaultValueOperatorTest.GetStringValue();

   7:  Console.WriteLine("Returns 'Unspecified' because s is null => {0}", s ?? "Unspecified");

5. Curry 及 Partial 方法

Curry方法是把接受七个参数的函数转变到接受二个纯净参数(最早函数的率先个参数)的函数,而且重临接受余下的参数且重返结果的新函数的技艺。

   1:  public static class CurryMethodExtensions

   2:  {

   3:      public static Func<A, Func<B, Func<C, R>>> Curry<A, B, C, R>(this Func<A, B, C, R> f)

   4:      {

   5:          return a => b => c => f;

   6:      }

   7:  }

倘诺果没有法鲜明成员函数的归来类型可使用Var推行。

Partial—Partial类型允许大家将二个类、接口或组织分为好几个部分,分别完成在多少个不等的.cs文件中。关键字partial是贰个上下文关键字,独有和class、struct、interface放在一齐时才有重要字的意思。由此partial的引入不会影响现存代码中名为partial的变量

   1:  public static class CurryMethodExtensions

   2:  {

   3:      public static Func<C, R> Partial<A, B, C, R>(this Func<A, B, C, R> f, A a, B b)

   4:      {

   5:          return c => f;

   6:      }

   7:  }

6. WeakReference

Weak Reference 表示项目化弱援引,即在引用对象的同一时间依旧允许垃圾回收来回收该对象。如果你想利用该援用,能够设置为强援引类型,有限支撑该对象不被回收。

   1:  WeakReferenceTest hugeObject = new WeakReferenceTest();

   2:  hugeObject.SharkFirstName = "Sharky";

   3:  WeakReference w = new WeakReference(hugeObject);

   4:  hugeObject = null;

   5:  GC.Collect();

   6:  Console.WriteLine((w.Target as WeakReferenceTest).SharkFirstName);

7. Lazy<T>

Lazy<T>提供对延缓一些大资源或财富紧张的对象的初阶化的支撑。在前后相继的生存期内,特别是在这种措施创立或实践只怕不发出使用延缓开头化延迟一种或大气占为己有财富的对象的创办、能源的天职的奉行。

   1:  public abstract class ThreadSafeLazyBaseSingleton<T>

   2:      where T : new()

   3:  {

   4:      private static readonly Lazy<T> lazy = new Lazy<T> => new T;

   5:      

   6:      public static T Instance

   7:      {

   8:          get

   9:          {

  10:              return lazy.Value;

  11:          }

  12:      }

  13:  }

8. BigInteger

BigInteger 类型 表示任意大的带符号整数。理论上来说,该项目标数目是不曾边界值限制的。该品种与其他整型类型区别,包涵MinValue和马克斯Value 属性。
   1:  string positiveString = "91389681247993671255432112000000";

   2:  string negativeString = "-90315837410896312071002088037140000";

   3:  BigInteger posBigInt = 0;

   4:  BigInteger negBigInt = 0;

   5:  

   6:  posBigInt = BigInteger.Parse(positiveString);

   7:  Console.WriteLine(posBigInt);

   8:  negBigInt = BigInteger.Parse(negativeString);

   9:  Console.WriteLine(negBigInt);

9. 未官方记录的 C# 关键字: __arglist __reftype __makeref __refvalue

第9条奖的严重性字官方文书档案并未记录,或许正在测量检验中。可是这一个重大字丰裕了Visual Studio 编辑器的职能,也能被辨认。

开垦人士能够选取__makeref关键字成立变量。使用__refvalue修饰变量能够从 TypedReference中收获变量值。__arglist关键字与params的魔法一样,能够访谈参数列表。

   1:  int i = 21;

   2:  TypedReference tr = __makeref;

   3:  Type t = __reftype;

   4:  Console.WriteLine(t.ToString;

   5:  int rv = __refvalue( tr,int);

   6:  Console.WriteLine;

   7:  ArglistTest.DisplayNumbersOnConsole(__arglist(1, 2, 3, 5, 6));

为了采纳__arglist, 须要定义ArglistTest 类**

   1:  public static class ArglistTest

   2:  {

   3:      public static void DisplayNumbersOnConsole(__arglist)

   4:      {

   5:          ArgIterator ai = new ArgIterator(__arglist);

   6:          while (ai.GetRemainingCount

   7:          {

   8:              TypedReference tr = ai.GetNextArg();

   9:              Console.WriteLine(TypedReference.ToObject;

  10:          }

  11:      }

  12:  }

10. Environment.NewLine

获得为此条件定义的换行字符串。

   1:  Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line", Environment.NewLine);

11. ExceptionDispatchInfo

代表捕获特定点的相当情形。能够使用ExceptionDispatchInfo.Throw 方法,命名空间为System.Runtime.ExceptionServices。

   1:  ExceptionDispatchInfo possibleException = null;

   2:  

   3:  try

   4:  {

   5:      int.Parse("a");

   6:  }

   7:  catch (FormatException ex)

   8:  {

   9:      possibleException = ExceptionDispatchInfo.Capture;

  10:  }

  11:  

  12:  if (possibleException != null)

  13:  {

  14:      possibleException.Throw();

  15:  }

12. Environment.FailFast()

假设想淡出程序,且没有须要调用任何Finally 块或Finalizers,能够行使Fail法斯特。

   1:  string s = Console.ReadLine();

   2:  try

   3:  {

   4:      int i = int.Parse;

   5:      if  Environment.FailFast("Special number entered");

   6:  }

   7:  finally

   8:  {

   9:      Console.WriteLine("Program complete.");

  10:  }

13. Debug.Assert & Debug.WriteIf & Debug.Indent

Debug.Assert——检查标准;如果基准为 false,则突显三个新闻框,当中会展现调用仓库。Debug.Assert尽在调节和测验版中央银行之有效,在公布版中只要要进行断言,则选取Trace.Assert。

Debug.Assert(1 == 0, "The numbers are not equal! Oh my god!");

借使Assert在Debug方式下战败,则会来得下图:

必赢的网址登录 3

14. Parallel.For & Parallel.Foreach

二十十六线程的意况下相比较常用。

Parallel.For—推行循环,迭代能够运作。

   1:  int[] nums = Enumerable.Range(0, 1000000).ToArray();

   2:  long total = 0;

   3:  

   4:  // Use type parameter to make subtotal a long, not an int

   5:  Parallel.For<long>(0, nums.Length, () => 0, (j, loop, subtotal) =>

   6:  {

   7:      subtotal += nums[j];

   8:      return subtotal;

   9:  },

  10:       => Interlocked.Add(ref total, x)

  11:  );

  12:  

  13:  Console.WriteLine("The total is {0:N0}", total);

Interlocked.Add 方法将八个整型值相加,并将结果保存到第二个数值,能够看成原子操作。

Parallel.Foreach——实施 foreach(在 Visual Basic 中为 For Each)操作,个中在 Partitioner 上恐怕会相互运营迭代。

必赢的网址登录 4

15. IsInfinity

归来三个值,该值提示钦赐数字是总计为负无穷大如故正无穷大。

   1:  Console.WriteLine("IsInfinity == {0}.", Double.IsInfinity ? "true" : "false");

16. 开辟工具

ComponentOne Studio for ASP.NET是一整套完备的开发工具包,用于在各种浏览器中创建和设计具有现代风格的Web应用程序。

原文链接:http://www.codeproject.com/Articles/1021335/Top-Underutilized-Features-of-NET

本文由必赢的网址登录发布于必赢网址,转载请注明出处:必赢的网址登录Top 15 不起眼却有大作用的 .NET功

关键词:

上一篇:  os.system是无法获取返回值的,  OS模块调用

下一篇:没有了