text
stringlengths
0
13.4k
Console.WriteLine(personA.Name); // Outputs 'Linda'
Console.WriteLine(personB.Name); // Outputs 'Bob'
}
}
Section 7.2: Reference types in use
Reference types are comprised of both a reference to a memory area, and a value stored within that area.
This is analogous to pointers in C/C++.
All reference types are stored on what is known as the heap.
The heap is simply a managed area of memory where objects are stored. When a new object is instantiated, a part
of the heap will be allocated for use by that object, and a reference to that location of the heap will be returned. The
heap is managed and maintained by the garbage collector, and does not allow for manual intervention.
In addition to the memory space required for the instance itself, additional space is required to store the reference
GoalKicker.com – .NET Framework Notes for Professionals
26
itself, along with additional temporary information required by the .NET CLR.
The code below demonstrates a reference type being assigned to a new variable. In this instance, we are using a
class, all classes are reference types (even if static).
When a reference type is assigned to another variable, it is the reference to the object that is copied over, not the
value itself. This is an important distinction between value types and reference types.
The implications of this are that we now have two references to the same object.
Any changes to the values within that object will be reflected by both variables.
class PersonAsReferenceType
{
public string Name;
}
class Program
{
static void Main()
{
PersonAsReferenceType personA;
personA = new PersonAsReferenceType { Name = "Bob" };
var personB = personA;
personA.Name = "Linda";
Console.WriteLine( // Outputs 'True' - because
object.ReferenceEquals( // personA and personB are referencing
personA, // the *same* memory location
personB));
Console.WriteLine(personA.Name); // Outputs 'Linda'
Console.WriteLine(personB.Name); // Outputs 'Linda'
}
GoalKicker.com – .NET Framework Notes for Professionals
27
Chapter 8: LINQ
LINQ (Language Integrated Query) is an expression that retrieves data from a data source. LINQ simplifies this
situation by offering a consistent model for working with data across various kinds of data sources and formats. In
a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform
data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a
provider is available. LINQ can be used in C# and VB.
Section 8.1: SelectMany (flat map)
Enumerable.Select returns an output element for every input element. Whereas Enumerable.SelectMany
produces a variable number of output elements for each input element. This means that the output sequence may
contain more or fewer elements than were in the input sequence.
Lambda expressions passed to Enumerable.Select must return a single item. Lambda expressions passed to
Enumerable.SelectMany must produce a child sequence. This child sequence may contain a varying number of
elements for each element in the input sequence.
Example
class Invoice
{
public int Id { get; set; }
}
class Customer