Jump to content
  • Advertisement
moeen k

why we have data type alias in c#?

Recommended Posts

hi. as you may know we have bool and Boolean  in c# that work completely the same. or single and float accept same values with same range and.......  is there any  reason for it? 

Share this post


Link to post
Share on other sites
Advertisement

Types like System.Float and System.Boolean come from the CTS (Common Type System), part of the CLI (Common Language Infrastructure) standard. This standard is defined in ECMA-335. They have nothing, per se, to do with C#.

C# is a language generally built on top of the CLI specification; it's standard (defined in ECMA-334) requires that a certain minimal subset of CLI be exposed through. That includes exposing the CTS types. The language then provides the common C-like aliases "bool" and "float" and such for aesthetics or convenience.

Share this post


Link to post
Share on other sites
2 hours ago, jpetrie said:

requires that a certain minimal subset of CLI be exposed through. That includes exposing the CTS types

thanks a lot for your answer but can you tell me why its required?

 

Share this post


Link to post
Share on other sites

By requiring it, the standard is ensuring that C# can be compiled to an intermediate representation any conforming CLI implementation can run, and also that it can inter-operate easily with other such languages (VB.NET, for example). This ability to inter-operate was a key design goal of the platform at the time; supporting it eases adoption and thus increases how quickly the ecosystem will spread.

I encourage you to read some of those linked standards (the introduction material in particular). They're considerably easier to digest than, say, the C++ standard.

Share this post


Link to post
Share on other sites
Posted (edited)

The ones with capital letters are blittable types. I can't go more into depth, but maybe, just maybe it's related that in C# the booleans, shorts, and bytes are represented with 4 bytes if on the stack, or maybe it was just the bools... and I don't know if it applies to structs on the stack. 
https://docs.microsoft.com/en-us/dotnet/framework/interop/blittable-and-non-blittable-types

Edited by RnzCpp

Share this post


Link to post
Share on other sites
On 3/17/2018 at 3:25 PM, RnzCpp said:

The ones with capital letters are blittable types.

This is not accurate. There is no difference between `System.Boolean` and `bool`. The latter is simply the C# language alias for the former, and there is no functional difference in their usage. I don't think there's anything dictating how much space primitive types take on the stack -- that is an implementation detail and likely depends on the runtime and optimization level being used.

Traditionally, `System.Boolean` is not considered a blittable type because the Win32 boolean type is 32-bits, whereas the .NET representation is 8-bytes. This behavior probably made sense when .NET was first designed, primarily for Win32 systems.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!