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.

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;



About bpostaci
Escalation Engineer in Microsoft.

Leave a Reply

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

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

Facebook photo

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

Connecting to %s

%d bloggers like this: