In this code:
using System;
namespace ConsoleApp1
{
public class TextInput
{
protected string _text = "";
public void Add(char c)
{
_text += c;
}
public string GetValue()
{
return _text;
}
}
public class NumericInput : TextInput
{
public new void Add(char c)
{
if (!char.IsDigit(c)) return;
_text += c;
}
}
public class Program
{
public static void Main(string[] args)
{
TextInput input = new NumericInput();
input.Add('1');
input.Add('a');
input.Add('0');
Console.WriteLine(input.GetValue());
Console.WriteLine(char.IsDigit('1'));
Console.WriteLine(char.IsDigit('a'));
Console.WriteLine(char.IsDigit('0'));
}
}
}
... calling Console.WriteLine(char.IsDigit('a')); returns correctly False but in the overridden Add method it always return True.
Obviously it calls TextInput.Add() instead of NumericInput.Add(). Can this be corrected inside the overriden Add() method? Code in Main may not change!