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

  • SEARCH
  • Home
  • 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 4170976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:23:46+00:00 2026-05-21T00:23:46+00:00

I have a struct in C# defined as follows(simplified for demonstration purposes), which I

  • 0

I have a struct in C# defined as follows(simplified for demonstration purposes), which I use in interop calls to a native DLL:

[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct MyStruct
{
    private byte val0;
    private byte val1;
    private byte val2;
    private byte val3;

    public MyStruct(UInt32 id)
    {
        this.val0 = (byte)id;
        id >>= 8;
        this.val1 = (byte)id;
        id >>= 8;
        this.val2 = (byte)id;
        id >>= 8;
        this.val3 = (byte)id;
    }

    public void Clear()
    {
        this.val0 = this.val1 = this.val2 = this.val3 = 0;
    }

    public override string ToString()
    {
        return
            this.val3.ToString("X2") + this.val2.ToString("X2") + "-" +
            this.val1.ToString("X2") + this.val0.ToString("X2");
    }
}

Update:
This struct is a property within another class. As follows:

public class MyClass
{
    MyStruct MyStruct { get; set; }
}

When I instantiate the struct with a value and follow this by a call to Clear(), the contents are not cleared. When I step into the Clear() method with the debugger I see each field getting cleared. Yet, once the call to Clear() completes, the original struct instance remains unaltered with its originally instantiated values.

MyClass class1 = new MyClass();
class1.MyStruct = new MyStruct(0x12345678);
System.Diagnostics.Debug.WriteLine("Init: " + class1.MyStruct.ToString());
class1.MyStruct.Clear();
System.Diagnostics.Debug.WriteLine("Clear: " + class1.MyStruct.ToString());

The above results in the following output:

Init: 1234-5678
Clear: 1234-5678

Why is this? Can one clear a struct after it has been instantiated?

Edit:
Sample code and results can be seen here: http://ideone.com/6szn9

Edit: Revised question to more accurately reflect the problem being related to the property accessor.

  • 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-21T00:23:46+00:00Added an answer on May 21, 2026 at 12:23 am

    Doesn’t do that for me. http://ideone.com/PDLnt

    Does your real code use a property by any chance? A property getter makes a copy when it returns.

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

Sidebar

Related Questions

Assume I have an array which holds some struct defined as follows: static struct
I have a struct defined like follows as part of an object. I'm trying
I have defined a custom struct which I need to send over to another
I have a struct defined as follows: template <typename T> struct data { int
I have a struct defined in a header as follows: #define LC_ERR_LEN 300 typedef
In a C struct I have defined a function pointer as follows: typedef struct
I have defined a Matrix module as follows: module Matrix = struct type 'a
I have a struct A that is defined as follows: typedef struct A {
Basically, I have to overload the << operator for my tokenType struct, which follows
I have a struct defined as follows struct VariableList { void Add(simple_instr* instr) {

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.