LosManos
LosManos

Reputation: 7692

Type renaming in C# with a renamed type

using IdType = System.Guid;
using RowType = System.Tuple<System.Guid, object>

works. while

using IdType = System.Guid;
using RowType = System.Tuple<IdType, object>

does not compile.
The IdType declared at first row cannot be used with further using it seems.

Is there a way around this?

Upvotes: 7

Views: 3855

Answers (2)

umasankar
umasankar

Reputation: 607

using IdType = System.Guid;
using RowType = System.Tuple<System.Guid, object>;

namespace WindowsFormsApplication1
{
public void Test()
{
  var guideq = typeof(IdType).Equals(typeof(System.Guid));
  var typeeq = typeof(RowType).Equals(typeof(System.Tuple<System.Guid,
  object>));
  System.Tuple<System.Guid, object> obj = new RowType(IdType.NewGuid(), "");
}

IdType and System.Guid is Same Type.

System.Tuple<System.Guid, object> and RowType is of the same type. In this situation it's just working as an alias name. So we need use any place.

In the code i am checking these two types for equality and that it is true; because it's alias name for the same type.

This type renaming (or) type alias name is helpful for coding, to understand and avoid confusion. Especially when types are getting complex.

Upvotes: 0

James Gaunt
James Gaunt

Reputation: 14783

This will work:

using IdType = System.Guid;
namespace x
{
    using RowType = System.Tuple<IdType, object>;
}

The reason being that type aliases only apply within declarations in the namespace within which they are contained.

Upvotes: 15

Related Questions