|
C# 2008 Programmer's ReferenceIf it isn't, a compiler error results. Like the ref keyword, you need to prefix the arguments with the out keyword when calling the function: int day, month, year; GetDate(out day, out month, out year); The this Keyword The this keyword refers to the current instance of an object (in a nonstatic class; discussed later in the section Static Classes). In the earlier section on methods, you saw the use of this: Console.WriteLine("{0} {1}", this.FirstName, this.LastName); While the FirstName and LastName variable could be referenced without using the this keyword, prefixing them with it makes your code more readable, indicating that you are referring to an instance member. However, if instance members have the same names as your parameters, using this allows you to resolve the ambiguity: public void SetName(string FirstName, string LastName) { this.FirstName = FirstName; this.LastName = LastName; } Another use of the this keyword is to pass the current object as a parameter to another method. For example: public class AddressBook { public void AddContact(Contact c) { Console.WriteLine(c.ID); Console.WriteLine(c.FirstName); Console.WriteLine(c.LastName); Console.WriteLine(c.Email); //---other implementations here--- //... } } The AddContact() method takes in a Contact object and prints out the details of the contact ...» |
Код для вставки книги в блог HTML
phpBB
текст
|
|