.NET Tips and Tricks

Blog archive

Properties with Parameters (and Making Them the Default)

Most developers aren't aware that you can write properties that accept parameters, just like a method does. Of course, it isn't often that you need a property that accepts a parameter. One way I recognize that I could use a property rather than a method is when my method name begins with the word "Get." Here, for example, is a method that (I assume) returns the specified part of a name:

Public Class NameManager
  Public Function GetNamePart(NamePosition As Integer) As String
    '...code
  End Function

If I rewrite the method as a property (and rename it), my class code looks like this:

Public Class NameManager
  Public ReadOnly Property NamePart(NamePosition As Integer) As String
    Get
      '...code
    End Get
  End Property

A developer who wants to get the customer's middle name would write code like this:

Dim nm As New NameManager("Peter Hunter Vogel")
Dim middleName As String
middleName = nm.NamePart(2)

You can go one step further and make this property the default property for the class, in which case a developer doesn't even have to use the property name. Here's the relevant code in Visual Basic (which adds the modifier Default to the property's signature):

Public Class NameManager
  Default Public ReadOnly Property NamePart(NamePosition As Integer) As String
    Get
      '...code
    End Get
  End Property

Here it is in C# (which renames the property to the keyword "this"):

public class NameManager
  public string this(int NamePosition) 
    get {
      '...code
   }

Regardless of how the class is written, the code to retrieve the middle name now looks like this:

middleName = nm(2)

Posted by Peter Vogel on 10/29/2015


comments powered by Disqus

Featured

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

  • TypeScript Tops New JetBrains 'Language Promise Index'

    In its latest annual developer ecosystem report, JetBrains introduced a new "Language Promise Index" topped by Microsoft's TypeScript programming language.

Subscribe on YouTube