How to set a default property for your class


I’m sorry but that functionality which is known by vb developers couldn’t possible when you are codding in c# .
Also there are some other solutions but its not possible to set a default property for your your class.

you can use indexer but its not that we want:

public string this[int idx]
{
get { … }
set { … }
}

or you can overload  implicit operator to do this but  is also not suggested.


[System.Diagnostics.DebuggerDisplay("{Value}")]
public class MyClass
{

public string Value { get; set; }
    public static implicit operator MyClass(string s)
    {
        return new MyClass { Value = s };
    }

    public static explicit operator string(MyClass f)
    {
        return f.Value;
    }
    public override string ToString()
    {
        return Value;
    }
}

 

Advertisement

About bpostaci
Escalation Engineer in Microsoft.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: