Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Real UseCase for using FieldOffSet considering the example

Real UseCase for using FieldOffSet considering the example

Scheduled Pinned Locked Moved C#
csharpdotnetperformancetutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ehsan Sajjad
    wrote on last edited by
    #1

    Hi, Reading CLR via C# on page 124 there is discussion about altering Layout kind for value types for InterOperability and there is example quoted which uses same FieldOffSet for multiple fields. I understand the the memory of both fields overlap and modifying one field will cause value of other field to be changed but what i am thinking about is the real use case where we would need it or have been used in past? here is the code for example:

    [StructLayout(LayoutKind.Explicit)]
    public struct SomeValType {
    [FieldOffset(0)]
    public Int16 m_b; // The m_b and m_x fields overlap each
    [FieldOffset(0)]
    public Int32 m_x; // other in instances of this type
    }
    }

    and when i wrote the following code in Main it caused to have 10 as value in the m_x field :

    SomeValType obj = new SomeValType();
    obj.m_b = 10;
    Console.WriteLine(obj.m_x);

    Can someone please shed some light regarding this. it will be highly appreciated. Thanks!

    OriginalGriffO 1 Reply Last reply
    0
    • E Ehsan Sajjad

      Hi, Reading CLR via C# on page 124 there is discussion about altering Layout kind for value types for InterOperability and there is example quoted which uses same FieldOffSet for multiple fields. I understand the the memory of both fields overlap and modifying one field will cause value of other field to be changed but what i am thinking about is the real use case where we would need it or have been used in past? here is the code for example:

      [StructLayout(LayoutKind.Explicit)]
      public struct SomeValType {
      [FieldOffset(0)]
      public Int16 m_b; // The m_b and m_x fields overlap each
      [FieldOffset(0)]
      public Int32 m_x; // other in instances of this type
      }
      }

      and when i wrote the following code in Main it caused to have 10 as value in the m_x field :

      SomeValType obj = new SomeValType();
      obj.m_b = 10;
      Console.WriteLine(obj.m_x);

      Can someone please shed some light regarding this. it will be highly appreciated. Thanks!

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Don't post the same question in multiple places - it just duplicates work and annoys people: Real usecase for same fieldoffset on multiple fields[^]

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      E 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Don't post the same question in multiple places - it just duplicates work and annoys people: Real usecase for same fieldoffset on multiple fields[^]

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        E Offline
        E Offline
        Ehsan Sajjad
        wrote on last edited by
        #3

        how to delete this one, as someone answered other place mine question, the delete button is disabled.

        OriginalGriffO 1 Reply Last reply
        0
        • E Ehsan Sajjad

          how to delete this one, as someone answered other place mine question, the delete button is disabled.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          You can't, because there is a reply. But...I can, so consider it done...

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups