Skip to Content
Learning C# 3.0
book

Learning C# 3.0

by Jesse Liberty, Brian MacDonald
November 2008
Beginner
696 pages
17h 43m
English
O'Reilly Media, Inc.
Content preview from Learning C# 3.0

Anonymous Types

From time to time you find yourself creating a class only so that you can create a single instance of it, never to use that class again. C# allows you to dispense with all that and combine object initializers and implicitly typed variables, which you learned about back in Chapter 3, to create a class with no name at all: an anonymous type.

You create an instance of an anonymous type with the keyword new, just as you would if you were instantiating an object of a declared class. Instead of passing parameters to a constructor, though, you use braces and define the member fields that you want your anonymous class to contain, like this:

new { Color = "Blue", Size = 13 }

The compiler creates a new class with two member fields. Notice that we’ve capitalized the names of the fields in this declaration, which is contrary to the naming scheme we’ve been using. That’s because these aren’t fields, but properties, which we’ll explain more fully in the next chapter. In brief, properties look like fields to the users of your class, and look like methods to the creator of your class.

This new class contains two properties, Color and Size. Just as with implicitly typed variables, the compiler can determine that Color is a String and Size is an int, and it types them accordingly. And as with implicitly typed variables, you can’t declare the property without assigning it a value, because the compiler needs that value to determine the property’s type.

Of course, the compiler does assign ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C# 3.0 Design Patterns

C# 3.0 Design Patterns

Judith Bishop
Windows Runtime via C#

Windows Runtime via C#

Jeffrey Richter and Maarten van de Bospoort
C# 7.0 in a Nutshell

C# 7.0 in a Nutshell

Joseph Albahari, Ben Albahari

Publisher Resources

ISBN: 9780596155018Errata Page