Pergunta

Considere o seguinte código:

public class TextType {

    public TextType(String text) {
        underlyingString = text;
    }

    public static implicit operator String(TextType text) {
        return text.underlyingString;
    }

    private String underlyingString;
}

TextType text = new TextType("Something");
String str = text; // This is OK.

Mas quero poder fazer o seguinte, se possível.

TextType textFromStringConstant = "SomeOtherText";

Foi útil?

Solução

public static implicit operator TextType(String text) {
    return new TextType(text);
}

Outras dicas

Adicionar

public static implicit operator TextType(string content) {
  return new TextType(content);
}

para sua aula? :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top