cs
- ///
/// 選擇性引數函式範例 /// - public partial class tsExampleMethod : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ExampleClass anExample = new ExampleClass();
- string a = anExample.ExampleMethod(1, "One", 1);
- string b = anExample.ExampleMethod(2, "Two");
- string c = anExample.ExampleMethod(3);
- Response.Write(a);
- Response.Write(b);
- Response.Write(c);
- }
- }
- class ExampleClass
- {
- private string _name;
- // Because the parameter for the constructor, name, has a default
- // value assigned to it, it is optional.
- public ExampleClass(string name = "Default name")
- {
- _name = name;
- }
- // The first parameter, required, has no default value assigned
- // to it. Therefore, it is not optional. Both optionalstr and
- // optionalint have default values assigned to them. They are optional.
- public string ExampleMethod(int required, string optionalstr = "default string",
- int optionalint = 10)
- {
- return string.Format("{0}: {1}, {2}, and {3}.", _name, required, optionalstr,
- optionalint);
- }
- }
沒有留言:
張貼留言