C# Base
- In a derived class, the base keyword can be used to access the same member in its base class.
- The base keyword can only be used in a constructor, a method of an instance or a property assessor of an instance.
- The base keyword is not allowed to be used in a static method.
- Comparing this with base, this keyword refers to the current instance of the class and base keyword refers to the instance of the base class of the current class.
Example 01-57-01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System; namespace TestBaseInConstructors { public class Person { protected string name; public Person() : this("None") { } public Person(string name) { this.name = name; } public void outName() { Console.Write("Name: {0}", this.name); } } public class Student : Person { protected int score; public Student() : this(0) { } public Student(int score) : base() { this.score = score; } public Student(string name, int score) : base(name) { this.score = score; } public void outScore() { outName(); Console.WriteLine("\tScore: {0}", this.score); } } class Program { static void Main(string[] args) { Student s = new Student(); s.outScore(); // Name: None Score: 0 s = new Student(80); s.outScore(); // Name: None Score: 80 s = new Student("Tim", 90); s.outScore(); // Name: Tim Score: 90 Console.Read(); } } }
Output
Name: None Score: 0 Name: None Score: 80 Name: Tim Score: 90
Explanation
- Line 5-22: Define a class Person with a string field, 2 constructors and a method.
- Line 9-11: Define the default constructor by using this keyword to call another constructor.
- Line 24-47: Define a class Student inherited from Person.
- Line 28-30: Define the default constructor by using this keyword to call Student(int score) constructor.
- Line 32-35: Define a constructor with one parameter score. In this constructor, base() is used to call the default constructor of the class Person.
- Line 37-40: Define a constructor with 2 parameters name and score. base(name) is used to call the constructor Person(string name) in class Person. Pay attention, base(name) is called at first then run the code in line 39 to assign the argument score to the field score.
- Line 42-46: Define a new method outScore().
- Line 44: Call the inherited method outName to output the name field.
- Line 49-63: Define a Program class for testing.
- Line 51: Main method starts running.
- Line 53: Instantiate Student class by calling its default constructor to set default values of the field name and score.
- Line 56: Instantiate Student class by calling its constructor with one parameter score to set the value of the field score.
- Line 59: Instantiate Student class by calling its constructor with 2 parameters to set the values of the field name and score.
- Line 54, 57, 60: Output the field values.
Example 01-57-02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System; namespace TestBaseInMethods { public class Pet { protected string name; public Pet() : this("None") { } public Pet(string name) { this.name = name; } public virtual void output() { Console.Write("Name: {0}", this.name); } } public class Dog : Pet { protected bool isBarking; public Dog(string name, bool isBarking) : base(name) { this.isBarking = isBarking; } public override void output() { base.output(); Console.WriteLine("\tBark: {0}", isBarking ? "Yes" : "No"); } } class Program { static void Main(string[] args) { Dog d = new Dog("Funny", true); d.output(); // Name: Funny Bark: Yes Console.Read(); } } }
Output
Name: Funny Bark: Yes
Explanation
- Line 5-22: Define public class Pet.
- Line 18-21: Define a virtual method output().
- Line 24-38: Define a public class Dog inheriting from Pet.
- Line 28-31: Define a constructor with 2 parameters. base(name) is used to call the constructor Pet(string name) in class Pet.
- Line 33-37: Define a overriding method output().
- Line 35: base.output() is used to call the virtual method output() defined in base class Pet to output pet's name.
- Line 40-48: Define a Program class for testing the defined classes Pet and Dog.
- Line 42: Main method starts running.
- Line 44: Create an instance of the class Dog by calling the instructor Dog(string name, bool isBarking).
- Line 45: Output the result.
What will be happened if the code in line 44 is changed to the follows.
Dog d = new Dog();
You will get the following error message.
'TestBaseInMethods.Dog' does not contain a constructor that takes 0 arguments
Because a constructor was already defined in line 28-31, the default constructor with 0 argument would not be created automatically by CLR. A default constructor will be created only when the class has no constructors.