当前位置: 首页>>代码示例>>C#>>正文


C# ParamArrayAttribute类代码示例

本文整理汇总了C#中System.ParamArrayAttribute的典型用法代码示例。如果您正苦于以下问题:C# ParamArrayAttribute类的具体用法?C# ParamArrayAttribute怎么用?C# ParamArrayAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ParamArrayAttribute类属于System命名空间,在下文中一共展示了ParamArrayAttribute类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Temperature

//引入命名空间
using System;

public class Temperature
{ 
   private decimal temp;
   
   public Temperature(decimal temperature)
   {
      this.temp = temperature;
   }
   
   public override string ToString() 
   {
      return ToString("C");
   }
   
   public string ToString(string format)
   {
      if (String.IsNullOrEmpty(format))
         format = "G";
      
      switch (format.ToUpper())
      {
         case "G":
         case "C":
            return temp.ToString("N") + "  °C";
         case "F":
            return (9 * temp / 5 + 32).ToString("N") + "  °F";
         case "K": 
            return (temp + 273.15m).ToString("N") + "  °K";
         default:
            throw new FormatException(String.Format("The '{0}' format specifier is not supported", 
                                                    format));
      }                                                         
   }         
   
   public void Display(params string []formats)
   {
      if (formats.Length == 0)
      {
         Console.WriteLine(this.ToString("G"));
      }
      else  
      { 
         foreach (string format in formats)
         {
            try {
               Console.WriteLine(this.ToString(format));
            }
            // If there is an exception, do nothing.
            catch { }
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:56,代码来源:ParamArrayAttribute

示例2: Main

public class Class1
{
   public static void Main()
   {
      Temperature temp1 = new Temperature(100);
      string[] formats = { "C", "G", "F", "K" }; 

      // Call Display method with a string array.
      Console.WriteLine("Calling Display with a string array:");
      temp1.Display(formats);
      Console.WriteLine();
      
      // Call Display method with individual string arguments.
      Console.WriteLine("Calling Display with individual arguments:");
      temp1.Display("C", "F", "K", "G");
      Console.WriteLine();
      
      // Call parameterless Display method.
      Console.WriteLine("Calling Display with an implicit parameter array:");
      temp1.Display();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:22,代码来源:ParamArrayAttribute

输出:

Calling Display with a string array:
100.00  °C
100.00  °C
212.00  °F
373.15  °K

Calling Display with individual arguments:
100.00  °C
212.00  °F
373.15  °K
100.00  °C

Calling Display with an implicit parameter array:
100.00  °C


注:本文中的System.ParamArrayAttribute类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。