Search This Blog

2009-06-03

C# 3.0 Tutorial -6:Type Equivalence

Type Equivalence

Type equivalence involves determining if two values are of the same type. In this case, we are concerned with type equivalence of objects instantiated from anonymous classes.

This comes up in practice when assignment is considered. Let's take an example.
var x = new {
Real = 5.4,
Complex = 2.8
};
var y = new {
Real = 1.9,
Complex = 5.3
};
x = y;Remember from the first part of the series that C# 3.0 is statically typed. That means that the variables x and y both have and retain a given type. Therefore, if the assignment is to work then y has to be of the same type as x (we don't have to consider subtyping here, since anonymous classes always inherit from object).

Two anonymous types will be considered equivalent if all of the following properties are true:
They have the same number of fields
They have fields of the same name declared in the same order
The types of each of the fields are identical
In the previous example, this is the case. However, any of the following changes to the anonymous type that was instantiated to give y will result in the types not being equivalent and the assignment resulting in a compile time error.
// Not equivalent due to an extra field.
var y = new {
Real = 1.9,
Complex = 5.3,
Conjugated = -5.3
};
// Not equivalent - fields in a different order.
var y = new {
Complex = 5.3,
Real = 1.9
};
// Not equivalent; different types (int != double)
var y = new {
Complex = 4,
Real = 2
};

Code Snippet(Aspx page)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CSharp3_TypeEquivalance : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Type Equivalance
var x = new { Real = 4.5, Complex = 2.8 };
//Not equivalent due to an extra field
var y1 = new { Real = 4.6, complex = 2.9, id = "123" };
//Not equivalent - fields in a different order
var y2 = new { Complex = 5.3,Real = 1.9};
// Not equivalent; different types (int != double)
var y3 = new{Complex = 4,Real = 2};


//have the same number of fields ,have fields of the same name declared in the same order ,The types of each of the fields are identical

var y4 = new { Real = 1.9, Complex = 5.3 };


Response.Write("x.GetType() == y1.GetType() :" + ((x.GetType() == y1.GetType()) ? "Yes" : "No,Not equivalent due to an extra field"));
Response.Write("<br/>");
Response.Write("x.GetType() == y2.GetType() :" + ((x.GetType() == y2.GetType()) ? "Yes" : "No,Not equivalent - fields in a different order"));
Response.Write("<br/>");
Response.Write("x.GetType() == y3.GetType() :" + ((x.GetType() == y3.GetType()) ? "Yes" : "No,Not equivalent; different types (int != double)"));
Response.Write("<br/>");
Response.Write("x.GetType() == y4.GetType() :" + ((x.GetType() == y4.GetType()) ? "Yes,have the same number of fields ,have fields of the same name declared in the same order ,The types of each of the fields are identical " : "No"));
Response.Write("<br/>");



if (x.GetType() == y1.GetType())
{
Response.Write("<br/>");
Response.Write(x.Real);
}
}
}

Output :

x.GetType() == y1.GetType() :No,Not equivalent due to an extra field
x.GetType() == y2.GetType() :No,Not equivalent - fields in a different order
x.GetType() == y3.GetType() :No,Not equivalent; different types (int != double)
x.GetType() == y4.GetType() :Yes,have the same number of fields ,have fields of the same name declared in the same order ,The types of each of the fields are identical

tags:what is Type Equivalence in 3.0/3.5,how can I implement Type Equivalence in c# 3.0/3.5

No comments: