Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
Use of explicit interface property by casting to generic type

I would like to call an explicit interface property by casting to a generic type. More specifically, here is the example I tried to make work:

public interface I1
{
    double Value { get; set; }
}

public interface I2 : I1
{
    // Hide value from I1 to use it as an explicit interface property.
    new double Value { get; set; } 
}

public class C1 : I2
{
    // Exception because I only care about I2 explicit Value property.
    double I1.Value { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

    double I2.Value { get; set; }
}

public class ExampleClass<T> where T : I1
{
    public double? SomeMethod(I1 i1)
    {
        if (i1 is T t)
            return t.Value;

        return null;
    }
}

public static class Main
{
    public static void Test()
    {
        var exampleClass = new ExampleClass<I2>();
        var c1 = new C1();
        ((I2)c1).Value = 1;
        exampleClass.SomeMethod(c1);
    }
}

If I call Main.Test(), he will throw an exception because he will try to get the I1.Value property on c1. Why is that case ? It seems to me he should cast c1 to I2, thus return the I2.Value property.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda