Sanjeeb Kumar

Sanjeeb Kumar

  • 1.6k
  • 47
  • 604

Show Conditional Compile Time error

Dec 29 2021 2:32 AM

Dear Sir/Mam,

Please have a look in my Name class below

which is intended to Use Like Built in sealed String Class : -

public class NameField 
{
    readonly string _value;

    public NameField(string value)
    {
        this._value = value;
        
    }
    public static implicit operator string(NameField d)
    {
        if (d._value.Length > 5) throw new System.Exception("");
        return d._value;
    }
    public static implicit operator NameField(string d)
    {
        return new NameField(d);
    }
}

now we can set values like:-

NameField myName="Sanjeeb Kumar Chaudhary"

Now I want to Show Conditional Compile Time error like below:-

  1. if string length is greater than 10 then "can not be greater than 10 characters "
  2. if string length is less than 5 then "can not be less than 5 characters "
  3. and much more as per requirements ....

Thus, kindly suggest how to do so ?


Answers (2)