开发者

How to add my own datatype in C# program?

开发者 https://www.devze.com 2023-01-07 17:07 出处:网络
Like \"int\" refers to \"Int32\" class, \"string\" refers to \"String\" class. H开发者_高级运维ow to refer a datatype like \"abc\" to my \"Abc\" class?Your \"class\" is a data type.

Like "int" refers to "Int32" class, "string" refers to "String" class. H开发者_高级运维ow to refer a datatype like "abc" to my "Abc" class?


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.


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.]


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.


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:

  • http://www.informit.com/articles/article.aspx?p=24456
  • http://en.wikipedia.org/wiki/Common_Type_System
  • http://msdn.microsoft.com/en-us/library/ms173104.aspx


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.


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.


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)]


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
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消