Home > Articles > Programming > C#

This chapter is from the book

This chapter is from the book

Programming with Anonymous Types

This chapter continues by exploring all of the ways you can use anonymous types, paving the way up to anonymous types returned by LINQ queries, stopping at the full explanation of the LINQ query here. You can simply think of the query as a first look at queries with the focus being on the anonymous type itself and what you can do with those types.

Defining Simple Anonymous Types

A simple anonymous type begins with the var keyword, the assignment operator (=), and a non-null initial value. The anonymous type is assigned to the name on the left side of the assignment operator, and the type emitted by the compiler to Microsoft Intermediate Language (MSIL) is determined by the right side of the operator. For instance:

var title = "LINQ Unleashed for C#";

uses the anonymous type syntax and assigns the string value to "LINQ Unleashed for C#". This code is identical in the MSIL to the following:

string title = "LINQ Unleashed for C#";

This emitted code equality can be seen by looking at the Intermediate Language (IL) with the Intermediate Language Disassembler (ILDASM) utility (see Figure 1.1).

Figure 1.1

Figure 1.1 Looking at the .locals init statement and the Console::Write(string) statement in the MSIL, it is clear that title is emitted as a string.

The support for declaring simple anonymous types exists more for completeness and symmetry than utility. In departmental language wars, purists are likely to rail against such use as it adds ambiguity to code. The truth is the type of the data is obvious in such simple use examples and it hardly matters.

Using Array Initializer Syntax

You can use anonymous type syntax for initializing arrays, too. The requirements are that the new keyword must be used. For example, the code in Listing 1.1 shows a simple console application that initializes an anonymous array of Fibonacci numbers. (The anonymous type and array initialization statement are highlighted in bold font.)

Listing 1.1. An Anonymous Type Initialized with an Array of Integers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ArrayInitializer
{
  class Program
  {
    static void Main(string[] args)
    {
      // array initializer
      var fibonacci = new int[]{ 1, 1, 2, 3, 5, 8, 13, 21 };
      Console.WriteLine(fibonacci[0]);
      Console.ReadLine();
    }
  }
}

The first eight numbers in the Fibonacci system are defined on the line that begins var fibonacci. Fibonacci numbers start with the number 1 and the sequence is resolved by adding the prior two numbers. (For more information on Fibonacci numbers, check out Wikipedia; Wikipedia is wicked cool at providing detailed facts about such esoterica.)

Even in the example shown in Listing 1.1, you are less likely to get involved in language ambiguity wars if you use the actual type int[] instead of the anonymous type syntax for arrays.

Creating Composite Anonymous Types

Anonymous types really start to shine when they are used to define composite types, that is, classes without the "typed" class definition. Think of this use of anonymous types as defining an inline class without all of the typing. Listing 1.2 shows an anonymous type representing a lightweight person class.

Listing 1.2. An Anonymous Type Containing Two Fields and Two Properties Without All of the Class Plumbing Typed By the Programmer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ImmutableAnonymousTypes
{
  class Program
  {
    static void Main(string[] args)
    {
      var dena = new {First="Dena", Last="Swanson"};
      //dena.First = "Christine"; // error - immutable
      Console.WriteLine(dena);
      Console.ReadLine();
    }
  }
}

The anonymous type defined on the line starting with var dena emits a class, referred to as a projection, in the MSIL (see Figure 1.2). Although the projection's name—the class name—cannot be referred to in code, the member elements—defined by the member declarators First and Last—can be used in code and IntelliSense works for all the elements of the projection (see Figure 1.3).

Figure 1.2

Figure 1.2 Anonymous types save a lot of programming time when it comes to composite types, as shown by the elements emitted to MSIL.

Figure 1.3

Figure 1.3 IntelliSense works quite well with anonymous types.

Another nice feature added to anonymous types is the overloaded ToString method. If you look at the MSIL or the output from Listing 1.2, you will see that the field names and field values, neatly formatted, are returned from the emitted ToString method. This is useful for debugging.

Adding Behaviors to Anonymous Composite Types

If you try to add a behavior to an anonymous type at initialization—for instance, by using an anonymous delegate—the compiler reports an error. However, it is possible with a little bending and twisting to add behaviors to anonymous types. The next section shows you how.

Adding Methods to Anonymous Types

To really understand language possibilities, it's helpful to bend and twist a language to make it do things it might not have been intended to do directly. One of these things is adding behaviors (aka methods). Although it might be harder to find a practical use for anonymous type–behaviors, Listing 1.4 shows you how to add a behavior to and use that behavior with an anonymous type. (The generic delegate Func in bold in the listing is used to initial the anonymous type's method.)

Listing 1.4. Adding a Behavior to an Anonymous Type

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;


namespace AnonysmousTypeWithMethod
{
  class Program
  {
    static void Main(string[] args)
    {
      // adding method possibility
      Func<string, string, string> Concat1 =
        delegate(string first, string last)
        {
          
          return last + ", " + first;
        
        };

      // whacky method but works
      Func<Type, Object, string> Concat2 =
        delegate(Type t, Object o)
        {
          PropertyInfo[] info = t.GetProperties();
          return (string)info[1].GetValue(o, null) +
            ", " + (string)info[0].GetValue(o, null);
        };

      var dena = new {First="Dena", Last="Swanson", Concat=Concat1};
      //var dena = new {First="Dena", Last="Swanson", Concat=Concat2};
      Console.WriteLine(dena.Concat(dena.First, dena.Last));
      //Console.WriteLine(dena.Concat(dena.GetType(), dena));
      Console.ReadLine();
    }
  }
}

The technique consists of defining an anonymous delegate and assigning that anonymous delegate to the generic Func class. In the example, Concat was defined as an anonymous delegate that accepts two strings, concatenates them, and returns a string. You can assign that delegate to a variable defined as an instance of Func that has the three string parameter types. Finally, you assign the variable Concat to a member declarator in the anonymous type definition (referring to var dena = new {First="Dena", Last="Swanson", Concat=Concat}; now).

After the plumbing is in place, you can use IntelliSense to see that the behavior—Concat—is, in fact, part of the anonymous type dena, and you can invoke it in the usual manner.

Using Anonymous Type Indexes in For Statements

The var keyword can be used to initialize the index of a for loop or the recipient object of a foreach loop. The former is a simple anonymous type and the latter becomes a useful construct when the container to iterate over is something more than a sample collection. Listing 1.5 shows a for statement, and Listing 1.6 shows the foreach statement, both using the var construct.

Listing 1.5. Demonstrating How to Iterate Over an Array of Integers—Using the Fibonacci Numbers from Listing 1.1—and the var Keyword to Initialize the Index

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AnonymousForLoop
{
  class Program
  {
    static void Main(string[] args)
    {
      var fibonacci = new int[]{ 1, 1, 2, 3, 5, 8, 13, 21 };
      for( var i=0; i<fibonacci.Length; i++)
        Console.WriteLine(fibonacci[i]);
      Console.ReadLine();
    }
  }
}

Listing 1.6. Demonstrating Basically the Same Code but Using the More Convenient foreach Construct

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AnonymousForEachLoop
{
  class Program
  {
    static void Main(string[] args)
    {
      var fibonacci = new int[]{ 1, 1, 2, 3, 5, 8, 13, 21 };
      foreach( var fibo in fibonacci)
        Console.WriteLine(fibo);
      Console.ReadLine();
    }
  }
}

The only requirement that must be met for an object to be the iterand in a foreach statement is that it must functionally represent an object that implements IEnumerable or IEnumerable<T>—the generic equivalent. Incidentally, this is also the same requirement for bindability, as in binding to a GridView.

An all-too-common use of the for construct is to copy a subset of elements from one collection of objects to a new collection, for example, copying all the customers in the 48843 ZIP code to a customersToCallOn collection. In C# 2.0, the yield return and yield break key phrases actually played this role. For example, yield return signaled the compiler to emit a state machine in MSIL—in essence, it emitted the copy collection for you.

In .NET 3.5, the ability to query collections, datasets, and XML to essentially ask questions about data or copy some elements is one of those things that LINQ does very well. Listing 1.7 shows code that uses a LINQ statement to return just the numbers in the Fibonacci short sequence that are divisible by 3. (For now, don't worry about understanding all of the elements of the query.)

Listing 1.7. A foreach Statement Whose Iterand Is Derived from a LINQ Query

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AnonymousForEachLoopFromExpression
{
  class Program
  {

    static void Main(string[] args)
    {
      var fibonacci = new int[]{ 1, 1, 2, 3, 5, 8, 13, 21, 33, 54, 87 };
      // uses LINQ query
      foreach( var fibo in from f in fibonacci where f%3==0 select f)
        Console.WriteLine(fibo);
      Console.ReadLine();

    }
  }
}

The LINQ query—used as the iterand in the foreach statement—makes up this part of the Listing 1.7:

from f in fibonacci where f % 3 == 0 select f

For now, it is enough to know that this query meets the requirement that it returns an enumerable result, in fact, IEnumerable<T> where T is an int type.

If this is your first experience with LINQ, the query might look strange. The capability and power and this book will quickly make them familiar and desirable friends. For now, it is enough to know that queries meet the requirement of an enumerable resultset and can be used in a foreach statement.

Anonymous Types and Using Statements

The using statement is shorthand notation for try...finally. With try...finally and using, the purpose is to ensure resources are cleaned up before the using block exits or the finally block is run. This is accomplished by calling Dispose, which implies that items created in using statements implement IDisposable. Employ using when the created types implement IDisposable—like SqlConnections—and use try...finally when you need to do some kind of cleanup work, but do not necessarily need to invoke Dispose (see Listing 1.8).

Listing 1.8. Using Statement and var Work Because SqlConnection Implements IDisposable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace AnonymousUsingStatement
{
  class Program
  {
    static void Main(string[] args)
    {
      string connectionString =
        "Data Source=BUTLER;Initial Catalog=AdventureWorks2000;" +
        "Integrated Security=True";
      using( var connection = new SqlConnection(connectionString))
      {
        connection.Open();
        Console.WriteLine(connection.State);
        Console.ReadLine();


      }
    }
  }
}

The help documentation will verify that SqlConnection is derived from DBConnection, which, in turn, implements IDisposable. You can use a tool like Anakrino or Reflector—free decompilers and disassemblers—to see that Dispose in DBConnection invokes the Close method on a connection.

To really understand how things are implemented, you can use ILDASM—or one of the previously mentioned decompilers—and look at the MSIL that is emitted. If you look at the code in Listing 1.8's IL, you can clearly see the substitution of using for a properly configured try...finally block. (The try element—after SqlConnection creation—and the finally block invoking Dispose are shown in bold font in Listing 1.9.)

Listing 1.9. The MSIL for the var and using Statement in Listing 1.8

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       66 (0x42)
  .maxstack  2
  .locals init ([0] string connectionString,
           [1] class [System.Data]System.Data.SqlClient.SqlConnection connection,
           [2] bool CS$4$0000)
  IL_0000:  nop
  IL_0001:  ldstr      "Data Source=BUTLER;Initial Catalog=AdventureWorks2"
  + "000;Integrated Security=True"
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008: newobj   instance void
ccc.gif[System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
  IL_000d:  stloc.1
  .try
  {
    IL_000e:  nop
    IL_000f:  ldloc.1
    IL_0010:  callvirtinstance void
[System.Data]System.Data.Common.DbConnection::Open()
    IL_0015:  nop
    IL_0016:  ldloc.1
    IL_0017:  callvirt   instance valuetype [System.Data]System.Data.ConnectionState
[System.Data]System.Data.Common.DbConnection::get_State()
    IL_001c:  box        [System.Data]System.Data.ConnectionState
    IL_0021:  call       void [mscorlib]System.Console::WriteLine(object)
    IL_0026:  nop
    IL_0027:  call       string [mscorlib]System.Console::ReadLine()
    IL_002c:  pop
    IL_002d:  nop
    IL_002e:  leave.s    IL_0040
  }  // end .try
  finally
  {
    IL_0030:  ldloc.1
    IL_0031:  ldnull
    IL_0032:  ceq
    IL_0034:  stloc.2
    IL_0035:  ldloc.2
    IL_0036:  brtrue.s   IL_003f
    IL_0038:  ldloc.1
    IL_0039:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
    IL_003e:  nop
    IL_003f:  endfinally
  }  // end handler
  IL_0040:  nop
  IL_0041:  ret
} // end of method Program::Main

You don't have to master IL to use .NET effectively, but you can learn from it and writing .NET emitters—code that emits IL directly—is supported in the .NET Framework. As shown in the MSIL, you can infer, for example, that the proper way to use try...finally is to create the protected object, try to use it, and, finally, clean it up. If you read a little further—in the finally block starting with IL 0030—you can see that the compiler also put a check in to ensure that the protected object, the SqlConnection, is compared with null before Dispose is called. This code is demonstrated in IL 0030, IL 0031, IL 0032, and the branch statement on IL 0036.

Returning Anonymous Types from Functions

Anonymous types can be returned from functions because the garbage collector (GC) cleans up any objects, but outside of the defining scope, the anonymous type is an instance of an object. Unfortunately, returning an object defeats the value of the IntelliSense system and the strongly typed nature of anonymous types. Although you could use reflection to rediscover the capabilities of the anonymous type, again you are taking a feature intended to make life more convenient and making it somewhat inconvenient again. Listing 1.10 puts these elements together, but as a practical matter, it is best to design solutions to use anonymous types within the defining scope. (Ironically, using objects within the defining scope was a style issue used in C++ to reduce the probability of memory leaks. Those familiar with C++ won't find this slight quirk of anonymous types any more inconvenient.)

Listing 1.10. Returning an Anonymous Type from a Method Defeats the Strongly Typed Utility of Anonymous Types

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;


namespace ReturnAnonymousTypeFromMethod
{
  class Program
  {
    static void Main(string[] args)
    {
      var anon = GetAnonymous();
      Type t = anon.GetType();
      Console.WriteLine(t.GetProperty("Stock").GetValue(anon, null));
      Console.ReadLine();
    }

    public static object GetAnonymous()
    {
      var stock = new {Stock="MSFT", Price="32.45"};
      return stock;
    }
  }
}

Although it is intellectually satisfying to play with the reflection subsystem, writing code like that in Listing 1.10 is a slow and painful means to an end. (In addition, the code in Listing 1.10, as written, is fraught with the potentiality for bugs due to null values being returned from GetType, GetProperty, and GetValue.)

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020