Calling the base constructor in C#
public class MyBaseClass {
MyBaseClass(T input) {
input.doSomething();
}
}
public class MyClass : MyBaseClass {
public MyClass() : base(input) {
//other stuff here
}
}
Calling the base constructor in C#
public class MyBaseClass {
MyBaseClass(T input) {
input.doSomething();
}
}
public class MyClass : MyBaseClass {
public MyClass() : base(input) {
//other stuff here
}
}
c# superclass constructor
// Create Constructor in superclass
public Vehicle( double speed )
{
Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
}
//Original Constructor
class Sedan
{
public Sedan(double speed)
{
Speed = speed;
LicensePlate = Tools.GenerateLicensePlate();
Wheels = 4;
}
// Remove 'Speed' and 'LicensePlate' from Sedan Constructor
// Add 'class Sedan : Vehicle '
// Add 'public Sedan(double speed) : base(speed)'
//New Constructor
class Sedan : Vehicle
{
public Sedan(double speed) : base(speed)
{
Wheels = 4;
}
}
c# base class without empty constructor
class A
{
public A(int x, int y)
{
// do something
}
}
class A2 : A
{
public A2() : base(1, 5)
{
// do something
}
public A2(int x, int y) : base(x, y)
{
// do something
// but no need to repeat the statements from the original constructor
}
// This would not compile:
public A2(int x, int y)
{
// the compiler will look for a constructor A(), which doesn't exist
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us