powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / implicit operator + interface
3 сообщений из 3, страница 1 из 1
implicit operator + interface
    #39607120
Фотография hVostt
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Смотрим код

Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
    public struct Some<T>
    {
        public T Value { get; }

        public Some(T t) => Value = t;

        public static implicit operator Some<T>(T value)
        {
            return new Some<T>(value);
        }
    }

    public interface ITest { }

    public class Test : ITest { }

    class Program
    {
        static void Main(string[] args)
        {
            Test test1 = new Test();
            ITest test2 = new Test();

            Some<int> some0 = 123; // OK
            Some<ITest> some1 = test1; // OK
            Some<ITest> some2 = test2; // ошибка.. почему???
        }
    }



Есть мысли?
...
Рейтинг: 0 / 0
implicit operator + interface
    #39607164
Сон Веры Павловны
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
C# Language Specification, §10.10.3 Conversion operators (ближе к концу):
User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type.
- вроде как оно.
...
Рейтинг: 0 / 0
implicit operator + interface
    #39607191
Фотография hVostt
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Сон Веры Павловны,

Да, точно оно. И понятно почему :)

Спасибо!
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / implicit operator + interface
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]