How to Resolve Error "CS1018: Keyword 'this' or 'base' expected" in C#
The Compiler Error CS1018 is a syntax error specific to Constructor Chaining. The message reads: "Keyword 'this' or 'base' expected".
In C#, constructors can call other constructors to avoid code duplication. This is done using an initializer list (a colon : followed by a call) placed between the constructor signature and its body. The strict syntax rule for this list is that you must use this (to call another constructor in the same class) or base (to call a constructor in the parent class). You cannot call constructors by using the class name directly.
This guide explains the correct syntax for chaining constructors in C#.
Understanding Constructor Chaining
When an object is instantiated, C# allows you to control the initialization sequence.
: this(...): Delegates initialization to another constructor within the current class.: base(...): Delegates initialization to a constructor in the base (parent) class.
The error CS1018 occurs when the compiler sees the colon : indicating a chain, but finds something other than this or base immediately after it.
Scenario 1: Calling Another Constructor in the Same Class
A common mistake, especially for developers coming from other languages, is trying to use the class name to call a sibling constructor.
Example of error
public class User
{
public string Name { get; set; }
public User(string name)
{
Name = name;
}
// ⛔️ Error CS1018: Keyword 'this' or 'base' expected.
// You cannot use the class name 'User' to chain constructors.
public User() : User("Default Name")
{
}
}
Solution: Use this
Replace the class name with the this keyword.
public class User
{
public string Name { get; set; }
public User(string name)
{
Name = name;
}
// ✅ Correct: 'this' refers to the current class's constructors.
public User() : this("Default Name")
{
}
}
Scenario 2: Calling a Parent Class Constructor
When inheriting from a class, you often need to pass arguments up to the parent's constructor. Using the parent class name explicitly in the initializer list is invalid.
Example of error
public class Animal
{
public Animal(int age) { }
}
public class Dog : Animal
{
// ⛔️ Error CS1018: Keyword 'this' or 'base' expected.
// You cannot write ': Animal(age)'.
public Dog(int age) : Animal(age)
{
}
}
Solution: Use base
Replace the parent class name with the base keyword.
public class Dog : Animal
{
// ✅ Correct: 'base' refers to the Animal constructor.
public Dog(int age) : base(age)
{
}
}
Scenario 3: The Hanging Colon
Sometimes this error is caused by a simple typo where a colon is typed, but the developer forgets to finish the statement or decides not to chain constructors but leaves the colon behind.
Example of error
public class Product
{
// ⛔️ Error CS1018: The compiler sees the ':' and expects 'this' or 'base'.
// It found the opening brace '{' instead.
public Product() :
{
// ...
}
}
Solution: Remove the Colon
If you do not intend to call a specific constructor (and are happy with the default base constructor), simply remove the colon.
public class Product
{
// ✅ Correct: No chaining syntax needed.
public Product()
{
// ...
}
}
Conclusion
CS1018 enforces the specific syntax for constructor delegation.
- Same Class: Use
: this(args)to call a sibling constructor. - Parent Class: Use
: base(args)to call a parent constructor. - Never: Use
: ClassName(args).