.NET Tips and Tricks

Blog archive

Using Dynamic Classes to Pass Data

In an earlier tip on using tuples to return multiple values from a method, several readers noted that the tuple's property names (Item1, Item2) left a lot to be desired -- you couldn't really call those names "self-documenting". As a fan of Really Obvious Code (ROC Rocks!), I should point out that there's a solution: using a Dynamic class. As with tuples, the goal here is to create a way to return multiple values from a method without defining a whole new class with its attendant properties and then using that class in exactly one place (returning values from a method) for just a few milliseconds.

A dynamic class lets you add properties to a class without defining them beforehand, as in this example which defines the variable returnClass as dynamic and then adds a CustomerID and Age property to it just by setting values on those properties:

private dynamic GetCustomerInfo()
  {
    dynamic returnClass;
    returnClass.CustomerID = "A123";
    returnClass.Age = 21;
    return returnClass;
  }

To call this method, accept the class, and access the properties, you'd write code like this:

 
dynamic aClass;
aClass = GetCustomerInfo();
this.CustIdTextBox.Text = aClass.CustomerID;

What you give up with Dynamic classes is IntelliSense: when accessing the properties on aClass, IntelliSense won't provide the list of property names for the class. However, compared to using tuples (with the distinctly unhelpful names Item1, Item2 and so on), that's not a real loss. What you do lose that you may care about, however, is compile-time type safety: if you set a dynamic property to a string and then attempt to move the value into an integer variable, you won't find about your problem until the code executes at runtime.

As another reader pointed out in that original column, neither technique is a good choice for a public interface, but worth considering when you have two private methods interacting.

Posted by Peter Vogel on 02/13/2012 at 1:23 PM


comments powered by Disqus

Featured

  • Visual Studio Code Dev Team Cleans Up

    The Visual Studio Code development team focused on some housekeeping in the October update, closing more than 4,000 issues on GitHub, where the cross-platform, open-source editor lives.

  • ML.NET Model Builder Update Boosts Image Classification

    Microsoft announced an update to the Model Builder component of its ML.NET machine learning framework, boosting image classification and adding "try your model" functionality for predictions with sample input.

  • How to Do Naive Bayes with Numeric Data Using C#

    Dr. James McCaffrey of Microsoft Research uses a full code sample and screenshots to demonstrate how to create a naive Bayes classification system when the predictor values are numeric, using the C# language without any special code libraries.

  • Vortex

    Open Source 'Infrastructure-as-Code' SDK Adds .NET Core Support for Working with Azure

    Pulumi, known for its "Infrastructure-as-Code" cloud development tooling, has added support for .NET Core, letting .NET-centric developers use C#, F# and VB.NET to create, deploy, and manage Azure infrastructure.

  • .NET Framework Not Forgotten: Repair Tool Updated

    Even though Microsoft's development focus has shifted to the open-source, cross-platform .NET Core initiative -- with the aging, traditional, Windows-only .NET Framework relegated primarily to fixes and maintenance such as quality and reliability improvements -- the latter is still getting some other attention, as exemplified in a repair tool update.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events