Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:22:41+00:00 2026-05-18T06:22:41+00:00

In C# I’ve got some safe API code related to UAC elevation. It involves

  • 0

In C# I’ve got some “safe” API code related to UAC elevation. It involves getting the size of an enum (as follows)

int myEnumSize = sizeof (MyEnum);

The code itself is valid, compiles, works correctly etc. But Resharper falsely flags it as a an error (“Cannot use unsafe construct in safe context”) within the solution. (Starting with version 2.0 of C#, applying sizeof to built-in types no longer requires that unsafe mode be used.) I love Resharper, and I love the solution analysis, but with this code in the solution I have a big red dot in the corner that makes me always think something is broken. If I tell resharper to ignore this error it comes back within minutes.

I would raise the issue with JetBrains, but I looked on their tracker and they’ve already got one logged that has been ignored since March. Looking further they have at least two other instances of this logged going back several years, both were dismissed with a “no-repro” status. I don’t want to sign-up to their tracker just to up-vote this bug. I could still end-up holding my breath for years. The fastest way forward is just to work-around the issue.

What is the best alternative that is still correct and has the least chance of causing a maintainer any trouble later on?

I could hard-code it to:

int myEnumSize = 4;  

Is there are more correct solution? — which doesn’t use sizeof(enum)?

Btw:

 Marshal.SizeOf() 

is completely “safe” but returns the wrong size.

PS. The code in questions is heavily influenced by the UACSelfElvation demo code from Microsoft. If you want more details. But I don’t think they are relevant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:22:42+00:00Added an answer on May 18, 2026 at 6:22 am

    Looks ugly, but may work:

    int myEnumSize = Marshal.SizeOf(Enum.GetUnderlyingType(typeof(MyEnum)));
    

    Edit by John Gietzen:
    Proof:

    enum Enum1 : sbyte { A, B, C, D }
    enum Enum2 : short { A, B, C, D }
    enum Enum3 : int { A, B, C, D }
    enum Enum4 : long { A, B, C, D }
    
    enum Enum5 : byte { A, B, C, D }
    enum Enum6 : ushort { A, B, C, D }
    enum Enum7 : uint { A, B, C, D }
    enum Enum8 : ulong { A, B, C, D }
    

    sizeof(Enum1): 1
    sizeof(Enum2): 2
    sizeof(Enum3): 4
    sizeof(Enum4): 8
    sizeof(Enum5): 1
    sizeof(Enum6): 2
    sizeof(Enum7): 4
    sizeof(Enum8): 8

    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum1))): 1
    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum2))): 2
    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum3))): 4
    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum4))): 8
    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum5))): 1
    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum6))): 2
    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum7))): 4
    Marshal.SizeOf(Enum.GetUnderlyingType(typeof(Enum8))): 8

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.