-2

Im sorry i just wrote something to get my point accross.

Here is an actual example code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CacheScratch
{
    class Class1
    {
        public const int a = 0xABAC;

        int test()
        {
            return sizeof(a);
        }
    }
}

If i have a const, why do i get the error: Severity Code Description Project File Line Suppression State Error CS0233 'a' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.Runtime.InteropServices.Marshal.SizeOf)

Why do I need a Marshal for a fixed size variable?

Git
  • 214
  • 5
  • 16
  • Your code isn't close to compiling. You're referencing a non-existing variable, you're not using a type argument and `0xA4B4` doesn't fit in a short. – Jeroen Vannevel Jul 11 '16 at 15:25
  • 1
    https://msdn.microsoft.com/en-us/library/eahchzkf(v=vs.120).aspx – Jim Hewitt Jul 11 '16 at 15:27
  • 8
    sizeof takes a type in C#, not an arbitrary expression. – Eric Lippert Jul 11 '16 at 15:28
  • There's really no reason you would even need to know this in .Net because it is managed code. – Adam Jul 11 '16 at 15:32
  • 2
    The size of an int is always 4 in C#. But why do you care what the size is? **Say what problem you are actually trying to solve here**. Odds are pretty good you're doing something wrong if you're trying to take the size of an integer in C#. – Eric Lippert Jul 11 '16 at 15:36
  • Possible duplicate of [sizeof() structures not known. Why?](http://stackoverflow.com/questions/8048540/sizeof-structures-not-known-why) – default Jul 11 '16 at 15:48
  • @EricLippert I updated the question. – Git Jul 12 '16 at 09:26
  • This is now a completely different question. Please don't do that. Delete the question and start an entirely new question if you have a completely different question. – Eric Lippert Jul 12 '16 at 12:07

1 Answers1

2

From MSDN: sizeof (C# Reference)

For all other types, including structs, the sizeof operator can be used only in unsafe code blocks. Although you can use the Marshal.SizeOf method, the value returned by this method is not always the same as the value returned by sizeof. Marshal.SizeOf returns the size after the type has been marshaled, whereas sizeof returns the size as it has been allocated by the common language runtime, including any padding.

Used to obtain the size in bytes for an unmanaged type****

Odds are that there is really no reason to need to obtain the size in bytes for a managed type, which is what you're doing.

"Why do I need a Marshal for a fixed size variable?"

You don't necessarily have to, if all you're looking for is the size of your local variable. You can simply call var size = sizeof(int) or sizeof(short) which will return 4 and/or 2 respectively.

Community
  • 1
  • 1
Adam
  • 2,422
  • 18
  • 29
  • My idea was, for readability i could use the sizeof() for my fixed size variables. Apparently not. – Git Jul 12 '16 at 08:08