Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] Avoid ToString() affect the message internal state #993

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amibar
Copy link

@amibar amibar commented Apr 25, 2024

Hi,

This PR intends to fix Issue 992.

The current generate ToString() implementation calls to this.BuildString() which change this state.

public override string ToString()
{
    var sb = new StringBuilder(100);
    this.BuildString(sb);
    return sb.ToString();
}

To avoid the affect ton this, I changed the generated ToString() to create a new instance of the message, wrap it and then use it with BuildString(). This way this is not affected.

public override string ToString()
{
    var sb = new StringBuilder(100);
    var m = new GroupAndVarLength();
    m.WrapForDecode(_buffer, _offset, _actingBlockLength, _actingVersion);
    m.BuildString(sb);
    return sb.ToString();
}

The assumption is that ToString() is only called for logging and debugging purposes, so the extra object created doesn't affect performance.

Ami

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C#] ToString() affects the message state
1 participant