P
P
Pavel Karinin2018-04-27 08:24:27
C++ / C#
Pavel Karinin, 2018-04-27 08:24:27

Parameterized property in c# - how to set?

Duplicate preamble from my previous question .
To make it clear where this question comes from: after a long experience working on vb.net (as my first language), relatively recently I had the 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.
In vb.net, I can define and successfully use properties with parameters, and you can define parameterized properties not only in classes, but also in interfaces, while parameters can be declared optional. This is a very handy feature when you need to return (or set) a value for a property that depends on something.
Here is an example on vb.net:

'''<summary>Отрасль промышленности</summary>
Public NotInheritable Class Industry : Implements IEnum(Of Byte)

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

  '''<summary>Наименование отраслевого направления</summary>
  '''<param name="Language">Язык, на котором следует вернуть наименование. Если язык не задан или он не поддерживается, то используется язык, назначенный в контекст текущего потока.</param>
  Public ReadOnly Property Name(Optional language As Language = Nothing) As String Implements IEnum(Of Byte).Name
    Get
      Return ...
    End Get
  End Property

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

End Class

Lyrical digression:
I already understood that c # is much inferior to the vb.net language in terms of semantics, but I did not expect that c # has such difficulties associated with seemingly elementary things. And what is done in vb.net "in one line", then in c# - this is a complete balancing act. The only plus that I noted for myself is the compactness of the code, which is dictated precisely by the structure of syntactic constructions, and not at all by something else. As a result, I come to the conclusion that in 99.(9)% vb.net is no less powerful language than c#. And my question is just one of the few on this topic.
Back to the question:
I looked at indexed properties, but did not understand how to implement them in relation to the case I described above. Do I understand correctly that this functionality needs to be implemented using indexes?, if so, how? Or am I still missing something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2018-04-27
@pavelkarinin

I completely agree that there is more beautiful syntactic sugar in VB .NET. And the possibilities are the same, because everything eventually compiles into the same result.
As for properties with parameters... In C#, there is no such thing without dancing with a tambourine, but why not use a regular function?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question