P
P
Pavel Karinin2018-04-27 07:25:48
C++ / C#
Pavel Karinin, 2018-04-27 07:25:48

Implementing an interface in c# - is it possible to use a different name for a property/method?

It was clear where such a question came from: after a long experience of working on vb.net (as my first language), relatively recently I had a need, and most importantly, the ability to master c # (from version 7.0) At the same time, having experience with JavaScript , I didn’t have any particular difficulties with mastering the c# syntax. Of course, I still constantly draw analogies with vb.net, since I often miss a lot of its features, which are not supported in c# a priori. And now, I would like to ask experts about one such feature.
vb.net defines an interface like this:

'''<summary>Определяет объект, являющийся элементом перечисления</summary>
'''<typeparam name="T">Тип идентификатора для элемента перечисления</typeparam>
Public Interface IEnum(Of T)

  '''<summary>Идентификатор элемента перечисления</summary>
  ReadOnly Property ID As T

  'и так далее...

End Interface

In the implementation of this interface, IDI can assign a different name to the property, which is semantically closer to this particular implementation and it will look like this:
'''<summary>Отрасль промышленности</summary>
Public NotInheritable Class Industry : Implements IEnum(Of Byte)

  '''<summary>Код отраслевого направления</summary>
  Public ReadOnly Property Code As Byte Implements IEnum(Of Byte).ID

  'и так далее...

End Class

or so, already in another implementation:
'''<summary>Сфера деятельности</summary>
Public NotInheritable Class Employment : Implements IEnum(Of Integer)

  '''<summary>Тип сферы деятельности</summary>
  Public ReadOnly Property Type As Integer Implements IEnum(Of Integer).ID

  'и так далее...

End Class

How, having defined the same interface in c#, can I use a different name for the property (in this case, for the property ID) in the classes that implement it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2018-04-27
@pavelkarinin

_
    public interface IId
    {
        int ID { get; set; }
    }

    public class Element : IId
    {
        public int Type
        {
            get
            {
                return ((IId)this).ID;
            }
        }

        int IId.ID { get; set; }
    }

A
Alexander Yudakov, 2018-04-27
@AlexanderYudakov

Explicit Interface Implementation (C# Programming ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question