简体   繁体   中英

When and why to use aliased name or its class name in c#?

Why and when should we use int , int16 , int32 , int64 , double , string instead of Int , Int16 , Int32 , Int64 , Double , String respectively? I have ready many articles about this but still not getting a proper solution.

This is purely a matter of preference/tradition.

C# language specification states:

Each of the predefined types is shorthand for a system-provided type. For example, the keyword int refers to the struct System.Int32. As a matter of style, use of the keyword is favoured over use of the complete system type name.

It truly does not matter, just be consistent. I personally use the original, lowercase names.

It doesn't matter at all in implementation . I personally prefer using the aliases, and from what I've seen of other code that's the more common choice. There are some who suggest using the BCL names ( Int32 etc) everywhere though - including Jeffrey Richter, author of CLR via C# (where he gives that advice).

However, when you're naming methods and types, you should use the BCL name rather than the C#-specific name. That way your code is equally idiomatic to people using your code from other languages. This is the convention adopted within the BCL itself. For example, we have Convert.ToInt32 and Convert.ToSingle instead of Convert.ToInt and Convert.ToFloat .

While it doesn't matter in theory if those are internal or private members, I'd suggest getting in the habit of giving your members the names you'd want to expose publicly - it means you can be consistent there regardless of access, and you're less likely to accidentally let one slip through.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM