Characteristics of Members of a Class |
|
Unlike C/C++, in C#, you can create a constant variable in a class. As done in Lesson 2 when studying variables, to declare a constant variable, type the const keyword to the left of the variable. Once again, when declaring a constant, you must initialize it with an appropriate constant value. If a class contains fields and methods, the (non-static) field members are automatically available to the method(s) of the class, even fields that are private. When accessing a field or a method from another method of the class, to indicate that the member you are accessing belongs to the same class, you can precede it with the this member and the period operator. Here are examples: |
public class House { public char PropertyType; public uint Bedrooms; public void Display() { Console.WriteLine("=//= Altair Realty =//="); Console.WriteLine("Properties Inventory"); ; Console.Write("Property Type: "); Console.WriteLine(this.PropertyType); Console.Write("Bedrooms: "); Console.WriteLine(this.Bedrooms); } } When using the this member variable (in C/C++, it is a pointer), you can access any member of a class within any method of the same class. There are rules you must observe when using this:
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | |
|