TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# base Keyword

Learn about the base-keyword and compare it to the this-keyword. Base refers to base class.
Base. This keyword is used in constructors (in constructor initializers). A derived class constructor is required to call the constructor from its base class.
Base, notes. When the default constructor isn't present, the custom base constructor can (with base) be referenced. This can eliminate duplicated code.Class
Base class example. The program uses a base class and a derived class. Both of the classes use a non-default, parameterful constructor.

Note: The derived class must use a base constructor initializer, with the base keyword, in its constructor declaration.

Info: Class A and class B have constructors. Class A is the parent or base class for class B, which is referred to as the derived class.

Syntax: The "B: A" syntax indicates that class B derives from class A. We use a colon character to indicate inheritance.

Inheritance

Explanation: In the example, the constructor in class B calls into the constructor of class A using base initializer syntax.

C# program that uses base constructor initializer using System; public class A // This is the base class. { public A(int value) { // Executes some code in the constructor. Console.WriteLine("Base constructor A()"); } } public class B : A // This class derives from the previous class. { public B(int value) : base(value) { // The base constructor is called first. // ... Then this code is executed. Console.WriteLine("Derived constructor B()"); } } class Program { static void Main() { // Create a new instance of class A, which is the base class. // ... Then create an instance of B. // ... B executes the base constructor. A a = new A(0); B b = new B(1); } } Output Base constructor A() Base constructor A() Derived constructor B()
Base versus this. Let us compare base and this. In a derived class, the base and this keywords can reference members. These keywords eliminate confusion as to which member we want.

Derived: When we have a derived class, we can use a "base" expression to directly access the base class.

Output: The program accesses first the base _value, which equals 6. And then it gets the this _value, which is 7.

Disambiguate: This is a fancy word that means "to make clear" which entity you are referring to.

C# program that uses base and this keywords using System; class Net { public int _value = 6; } class Perl : Net { public new int _value = 7; public void Write() { // Show difference between base and this. Console.WriteLine(base._value); Console.WriteLine(this._value); } } class Program { static void Main() { Perl perl = new Perl(); perl.Write(); } } Output 6 7
Notes, ambiguous. We use these keywords to resolve ambiguous things. If the base and this keywords were removed, the compiler would not know the difference between _value fields.

Constructors: The "base" and "this" keywords are also used in constructor initializers. These make constructors easier to write.

Thus: Base and this are needed for navigating the class hierarchy. With them, we access members from a targeted class.

This initializer. There is another keyword that can be used in a constructor initializer. You can use this() with the argument list of another constructor declaration in the same type.Constructor

Tip: This does the same thing conceptually as base but for the same class, not the parent class.

A summary. The base initializer is similar to the this-initializer in constructors. We can specify the base initializer when deriving from types with non-default constructors.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf