8

Like "int" refers to "Int32" class, "string" refers to "String" class. How to refer a datatype like "abc" to my "Abc" class?

8
  • 8
    why would you even want to do that? Jul 19, 2010 at 10:55
  • 9
    @Mitch Wheat - maybe to have a blue font in VS ;)
    – prostynick
    Jul 19, 2010 at 10:57
  • 4
    Give your class a correct name to begin with...A class IS a datatype! Jul 19, 2010 at 11:28
  • 2
    How is this getting so many upvotes and favorites? It doesn't make sense...
    – cjk
    Jul 19, 2010 at 11:48
  • 1
    @Piotr Kula : see the accepted answer Jun 12, 2022 at 2:16

8 Answers 8

20

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.

3
  • I think I should have used CTS here rather than CLR. My bad. Jul 19, 2010 at 11:02
  • But visual studio detects my class as a "class" and colored like a class. But datatypes colored different in Visual Studio. Jul 19, 2010 at 11:16
  • 5
    Incorrect. All classes are data types. VS highlights C# reserved words in dark blue and all other data types in pale blue. THe dark blue highlight is nothing to do with a difference between classes and types. Jul 19, 2010 at 11:26
10

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

6
  • Creepy...I even used the same namespace name
    – fletcher
    Jul 19, 2010 at 11:01
  • @fletcher: aye, that is creepy! Especially since I would never do this! lol! Jul 19, 2010 at 11:01
  • Suppose I want to give my class to other programmers to use. If I make my Class like a datatype then they can easily understand and use it like a simple datatype. Thanks for your answer. Jul 19, 2010 at 11:08
  • As Philip Smith pointed out, your custom class is a data type, and it's members become accessible through that context.
    – Hardryv
    Jul 21, 2010 at 18:03
  • long time ago, but the answer "WHY" is: Global define a data type like C for quickly change the type. e.g: DBKey = System.UInt32 or DBKey = System.UInt64 ... or DBKey=System.Int32
    – raiserle
    Jul 21, 2018 at 11:34
5
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.

0
5

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:

2

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.

0
0

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.

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.