Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






TheSystem.Tuple Type

In the System namespace, Microsoft has defined several generic Tuple types (all derived from Object) that differ by arity (the number of generic parameters). Here is what the simplest and most complex ones essentially look like.

 

// This is the simplest: [Serializable]

public class Tuple<T1> { private T1 m_Item1;

public Tuple(T1 item1) { m_Item1 = item1; } public T1 Item1 { get { return m_Item1; } }

}

 

// This is the most complex: [Serializable]

public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> {

private T1 m_Item1; private T2 m_Item2; private T3 m_Item3; private T4 m_Item4; private T5 m_Item5; private T6 m_Item6; private T7 m_Item7; private TRest m_Rest;

 

public Tuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7 item7, TRest rest) {

m_Item1 = item1; m_Item2 = item2; m_Item3 = item3; m_Item4 = item4; m_Item5 = item5; m_Item6 = item6; m_Item7 = item7; m_Rest = rest;

}

 

public T1 Item1 { get { return m_Item1; } } public T2 Item2 { get { return m_Item2; } } public T3 Item3 { get { return m_Item3; } } public T4 Item4 { get { return m_Item4; } } public T5 Item5 { get { return m_Item5; } } public T6 Item6 { get { return m_Item6; } } public T7 Item7 { get { return m_Item7; } } public TRest Rest { get { return m_Rest; } }

}


Like anonymous types, after a Tuple is created, it is immutable (all properties are read-only). I don’t show it here, but the Tuple classes also offer CompareTo, Equals, GetHashCode, and ToString methods, as well as a Size property. In addition, all the Tuple types implement the IStructural­ Equatable, IStructuralComparable, and IComparable interfaces so that you can compare two Tuple objects with each other to see how their fields compare with each other. Refer to the SDK docu- mentation to learn more about these methods and interfaces.

Here is an example of a method that uses a Tuple type to return two pieces of information to a caller.

 

// Returns minimum in Item1 & maximum in Item2

private static Tuple<Int32, Int32> MinMax(Int32 a, Int32 b) { return new Tuple<Int32, Int32>(Math.Min(a, b), Math.Max(a, b));

}

 

// This shows how to call the method and how to use the returned Tuple private static void TupleTypes() {

var minmax = MinMax(6, 2);

Console.WriteLine("Min={0}, Max={1}", minmax.Item1, minmax.Item2); // Min=2, Max=6

}

 

Of course, it is very important that the producer and consumer of the Tuple have a clear under- standing of what is being returned in the Item# properties. With anonymous types, the properties are given actual names based on the source code that defines the anonymous type. With Tuple types, the properties are assigned their Item# names by Microsoft and you cannot change this at all. Unfortunately, these names have no real meaning or significance, so it is up to the producer and consumer to assign meanings to them. This also reduces code readability and maintainability so you should add comments to your code explaining what the producer/consumer understanding is.



The compiler can only infer generic types when calling a generic method, not when you are call- ing a constructor. For this reason, the System namespace also includes a non-generic, static Tuple class containing a bunch of static Create methods that can infer generic types from arguments. This class acts as a factory for creating Tuple objects, and it exists simply to simplify your code. Here is a rewrite of the MinMax method shown earlier using the static Tuple class.

 

// Returns minimum in Item1 & maximum in Item2

private static Tuple<Int32, Int32> MinMax(Int32 a, Int32 b) {

return Tuple.Create(Math.Min(a, b), Math.Max(a, b)); // Simpler syntax

}

 

If you want to create a Tuple with more than eight elements in it, then you would pass another

Tuple for the Rest parameter as follows.

 

var t = Tuple.Create(0, 1, 2, 3, 4, 5, 6, Tuple.Create(7, 8));

Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}",

t. Item1, t.Item2, t.Item3, t.Item4, t.Item5, t.Item6, t.Item7, t.Rest.Item1.Item1, t.Rest.Item1.Item2);


 

NoteIn addition to anonymous types and the Tuple types, you might want to take a look at the System.Dynamic.ExpandoObject class (defined in the System.Core.dll assem- bly). When you use this class with C#’s dynamic type (discussed in Chapter 5, “Primitive, Reference, and Value Types”), you have another way of grouping a set of properties (key/ value pairs) together. The result is not compile-time type-safe, but the syntax looks nice (although you get no IntelliSense support), and you can pass ExpandoObject objects between C# and dynamic languages like Python. Here’s some sample code that uses an ExpandoObject.

dynamic e = new System.Dynamic.ExpandoObject();

e.x = 6; // Add an Int32 'x' property whose value is 6

e.y = "Jeff"; // Add a String 'y' property whose value is "Jeff"

e.z = null; // Add an Object 'z' property whose value is null

 

// See all the properties and their values: foreach (var v in (IDictionary<String, Object>)e)

Console.WriteLine("Key={0}, V={1}", v.Key, v.Value);

 

// Remove the 'x' property and its value var d = (IDictionary<String, Object>)e; d.Remove("x");

 


Date: 2016-03-03; view: 618


<== previous page | next page ==>
Nbsp;   Parameterless Properties | Nbsp;   Parameterful Properties
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)