Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Indirectly having interface property

I have a interface like this:

interface IName
    {
        string Name { get; set; }
    }

and a Class like this:

public class ClassN
    {
        public int N { get; set; }
    }

N in that Class is a int, and the interface requires a string Name.
I want the int N to be converted to string and be counted as Name in the interface.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So that when i do T.name i get the .N.toString().

Is it possible?

Name = N.ToString();

Example:

N = 2; than Name = 2; (but as string)

P.S: Sry the question was confusing. Tried again.

>Solution :

You can use an explicit interface implementation:

public class ClassN : IName
{
    string IName.Name
    {
         get { return N.ToString(); }
         set{ if(int.TryParse(value, out int i)) N = i; }
    }
    
    public int N { get; set; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading