The sizeof operator doesn't work on Int128 - C#

Shervan360 1,661 Reputation points
2024-08-26T11:09:34.5333333+00:00

Hello,

What are the differences between Int128 and other Int groups such as Int16, Int32, and Int64?

The sizeof operator doesn't work on Int128.

Screenshot 2024-08-26 141219

Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2024-08-26T12:01:31.33+00:00

    As VS and doc say, it is not a predefined type

    It works inside unsafe { } or

    with Marshal.SizeOf(typeof(Int128) (or Marshal.SizeOf<Int128>())


  2. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-09-17T08:27:31.9933333+00:00

    Hi @Shervan360 ,

    1. The sizeof operator works with Int16, Int32, and Int64 because they are common, well-defined primitive types in C#.
    2. For Int128, which is a new type introduced in .NET 7, the sizeof(Int128) is not allowed directly in safe code. This is because Int128 is not yet widely used or supported in the same way as other primitive types. To use sizeof with Int128, it must be in an unsafe context in C#, where pointers are allowed.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.