class ParameterClass { public string Name; public int IDNumber; public ParameterClass() { //set some default values; //since both fields are public they can //be overridden if necessary this.Name = ""; this.IDNumber = 0; } } class OptionalParameters { [STAThread] static void Main(string[] args) { //instantiate a parameter class object //and override the name field ParameterClass c = new ParameterClass(); c.Name = "Lamont Adams"; optionalObject(c); //show that the changed ID came back Console.WriteLine("c.IDNumber={0}",c.IDNumber); //call the method with only defaults optionalObject(new ParameterClass()); //pause so we can see the output Console.ReadLine(); } public static void optionalObject(ParameterClass arg) { //because the parameters received are encapsulated //in an object, they are all optional but have //a valid state even if not explicitly set by the caller Console.WriteLine("arg.Name={0}, arg.IDNumber={1}", arg.Name, arg.IDNumber); //change one of the field values arg.IDNumber = 10; } } |