2015年12月10日 星期四

ASP.NET 選擇性引數函式範例

ASP.NET 選擇性引數函式範例


cs
  1. ///
  2. /// 選擇性引數函式範例
  3. ///
  4. public partial class tsExampleMethod : System.Web.UI.Page
  5. {
  6. protected void Page_Load(object sender, EventArgs e)
  7. {
  8. ExampleClass anExample = new ExampleClass();
  9. string a = anExample.ExampleMethod(1, "One", 1);
  10. string b = anExample.ExampleMethod(2, "Two");
  11. string c = anExample.ExampleMethod(3);
  12.  
  13. Response.Write(a);
  14. Response.Write(b);
  15. Response.Write(c);
  16. }
  17. }


  1. class ExampleClass
  2. {
  3. private string _name;
  4.  
  5. // Because the parameter for the constructor, name, has a default
  6. // value assigned to it, it is optional.
  7. public ExampleClass(string name = "Default name")
  8. {
  9. _name = name;
  10. }
  11.  
  12. // The first parameter, required, has no default value assigned
  13. // to it. Therefore, it is not optional. Both optionalstr and
  14. // optionalint have default values assigned to them. They are optional.
  15. public string ExampleMethod(int required, string optionalstr = "default string",
  16. int optionalint = 10)
  17. {
  18. return string.Format("{0}: {1}, {2}, and {3}.", _name, required, optionalstr,
  19. optionalint);
  20.  
  21. }
  22. }

沒有留言:

張貼留言