Reputation: 2367
Like "int" refers to "Int32" class, "string" refers to "String" class. How to refer a datatype like "abc" to my "Abc" class?
Upvotes: 7
Views: 34053
Reputation: 3742
Q: Why would someone want to do this?
A: To produce self documented code.
using InternalLoggerName = System.String;
using ExternalLoggerName = System.String;
namespace LoggingUtils
{
public static class LoggerPool
{
private static readonly
ConcurrentDictionary
<
ExternalLoggerName
, InternalLoggerName
> LoggerNameMappings
= new ConcurrentDictionary<ExternalLoggerName, InternalLoggerName>();
public static ILog GetLogger(string loggerName)
{
// and you don't have to expose the aliases to the outside world
}
}
}
Upvotes: 0
Reputation: 179
there is no way to define custom datatypes in c# i have the same problem and i searched for the solution with no success in my case i need to define a datatype for unmanaged types like MarshalAs(UnmanagedType.LPWStr)]
Upvotes: 0
Reputation: 1492
The feature you are looking for is probably something you are used to from C++. There is no equivalent concept in C#. All you have are the builtin data type. The only thing you can declare is you own class or struct, but not datatype.
Upvotes: 0
Reputation: 33388
You're completely misunderstanding what a "data type" is. In C#, keywords like int
, string,
etc. are simply aliases for the corresponding types (implemented as classes/structs) already present in the CLR. For example, int
has exactly the same meaning as System.Int32
, which is a struct defined by the core of the .NET framework. Similarly, string
simply means System.String
, which is a class.
In .NET, every "data type" eventually inherits from System.Object
(which is aliased as object
in C#). The data types you refer to are simply pre-implemented classes and structs that inherit from System.Object
; there's nothing special about them. You should realize that C# does not have special primitive types in the same way that other languages do: they're all just part of a common type hierarchy. The keywords you're used to are simply provided as a convenience.
In essence, don't worry about it. Your classes can be used as they are, and this is how they are supposed to be used.
Some reading:
Upvotes: 5
Reputation: 300719
You can add an alias like this:
using abc = MyNamespace.Abc;
But I would question why you would want to do this.
[Another poster pointed out a valid use, namely namespace type clashes, but then I would always use the fully qualified type name otherwise it might get very confusing.]
Upvotes: 10
Reputation: 2801
Your "class" is a data type.
The examples you give are the difference between CLR data type names and C# datatype names. They are aliases. C# int maps to CLR Int32 and C# string maps to CLR String.
You can create your own aliases by using "using Xyx=Abc". You must do this in each source file, so it is not that useful.
Upvotes: 20
Reputation: 13780
using abc = MyNamespace.Abc;
I'm not sure what the advantage of this would be, it's usually used if you find different types with the same name.
Upvotes: 5
Reputation: 472
types like int, etc are build in types / reserved keywords. Those are defined by the compiler, so it's not possible to add your own.
Upvotes: 2