C# abstract class constructor code example
Example 1: c# abstract class
abstract class Shape
{
public abstract int GetArea();
}
class Square : Shape
{
int side;
public Square(int n) => side = n;
public override int GetArea() => side * side;
static void Main()
{
var sq = new Square(12);
Console.WriteLine($"Area of the square = {sq.GetArea()}");
}
}
Example 2: c# class inheritance constructor
public otherclass{
public otherclass(a,b){
a=0;
b=1;
}
}
public baseClass : otherclass{
public baseClass(string str,int a, int b): base(a,b){
str="whatever";
}
}
Example 3: what is abstract class in c#
Abstract class: is a restricted class that cannot be used to create objects
(to access it, it must be inherited from another class).
Example 4: c# class constructor
public class MyClass
{
public readonly int var1;
public readonly string var2;
public MyClass(int param1, string param2)
{
var1 = param1;
var2 = param2;
}
}
MyClass Class1 = new MyClass(10, "Hello, World!");
MyClass Class2 = new MyClass(9999, "This is the second object!");
Console.WriteLine(Class1.var1);
Console.WriteLine(Class2.var2);