5

It's possible to define an alias in C# like this

using kvp = System.Collections.Generic.KeyValuePair<string, string>;

var pair = new kvp("key", "value");

Microsoft define aliases too:

        int i;
        Int32 i2;

How can we define aliases that are available within a namespace? Is this configurable?


This question is specifically about an alias... so... using inheritance as a proxy isn't desired. I'm happy with that in many situations... but not when you want the best of both descriptive names and a shorthand version.

sgtz
  • 8,849
  • 9
  • 51
  • 91
  • 6
    The reason you can use `int` anywhere is because it's a C# keyword as defined in the language, not an alias as defined with a `using`. `Int32` is usually just `System.Int32`, assuming `using System;` is already present. Also, `int` maps directly to `System.Int32`. – BoltClock Jun 24 '12 at 11:27
  • 1
    and more on the topic is over here: http://stackoverflow.com/questions/62503/c-int-or-int32-should-i-care, one solution off the top of my head is you could have a class that derives from a base class with a different name. – Jeremy Thompson Jun 24 '12 at 11:30
  • And another one: [No, there's no equivalent of typedef](http://stackoverflow.com/a/161484/706456) – oleksii Jun 24 '12 at 12:12
  • 2
    VB.NET has it, C++/CLI has it, C# doesn't have it. – Hans Passant Jun 24 '12 at 19:25
  • 1
    @HansPassant: would you like to propose this as an answer? – sgtz Jun 25 '12 at 12:53
  • possible duplicate of [Does C# Support Project-Wide Default Namespace Imports Like VB.NET?](http://stackoverflow.com/questions/789239/does-c-sharp-support-project-wide-default-namespace-imports-like-vb-net) – nawfal May 04 '13 at 06:43

2 Answers2

5

I don't think that what you're asking for is really possible. Here's a workaround: include a type called kvp that is a copy of KeyValuePair<string, string> and implicitly converts to and from your type.

public struct kvp
{
    public string Key { get; private set; }
    public string Value { get; private set; }

    public kvp(string key, string value)
        : this()
    {
        Key = key;
        Value = value;
    }
    public override string ToString()
    {
        return ((KeyValuePair<string, string>)this).ToString();
    }

    public static implicit operator KeyValuePair<string, string>(kvp k)
    {
        return new KeyValuePair<string, string>(k.Key, k.Value);
    }
    public static implicit operator kvp(KeyValuePair<string, string> k)
    {
        return new kvp(k.Key, k.Value);
    }
}

This has the effect of you being able to use kvp instead of KeyValuePair<string, string>, with no unintended effects in most cases.

If the type you wished to typedef were an unsealed class, you could do (something very close to) what you want by making a class that extends it, with all of the base class's constructors mirrored and extending base(...).

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • 1
    +1 This is what I would do too. I also recommend implementing the Equals(), GetHashCode(), ==() and !=() operators. You could hold a KeyValuePair inside instead of key string, value string too. – Danny Varod Jun 24 '12 at 18:59
  • Also, if you don't want to enable implicit casting (if you want to force consumers to use your type), use explicit operators instead. – Danny Varod Jun 24 '12 at 19:05
4

You are explicitly asking for an alias and not for a workaround. Therefore, the only answer I have is: There is no way to do this.

The using alias that you gave as an example is per file. C# does not have a construct that allows cross-file aliases.

usr
  • 168,620
  • 35
  • 240
  • 369