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 5843903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:13:08+00:00 2026-05-22T12:13:08+00:00

Short question: When I put one and the same instance of an object inside

  • 0

Short question: When I put one and the same instance of an object inside viewstate twice, upon deserialization there are two instances. I want there to be just one instance. Can this be done and how?

Wordy explanation:

Consider the following code:

public partial class MyControl : System.Web.UI.UserControl
{
    [Serializable]
    class MyClass
    {
        public string x;
    }
    public void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            MyClass a = (MyClass)this.ViewState["a"];
            MyClass b = (MyClass)this.ViewState["b"];
            MessageManager.Show((a == b).ToString(), MessageSeverity.Debug);
        }
        else
        {
            var x = new MyClass() { x = "stackoverflow" };
            this.ViewState["a"] = x;
            this.ViewState["b"] = x;
            MessageManager.Show("Init", MessageSeverity.Debug);
        }
    }
}

When it is run, and a postback is initiated, I get the message “false”. That is, although I put a single object inside the viewstate, it got serialized twice. This can be verified by inspecting viewstate contents.

If I try to put cross-references objects in viewstate, then each item gets serialized as separate graph. To illustrate:

public partial class MyControl : System.Web.UI.UserControl
{
    [Serializable]
    class MyClass
    {
        public string x;
        public MyClass other;
    }
    public void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            MyClass a = (MyClass)this.ViewState["a"];
            MyClass b = (MyClass)this.ViewState["b"];
            MessageManager.Show((a.other == b).ToString(), MessageSeverity.Debug);
            MessageManager.Show((a.other.other == a).ToString(), MessageSeverity.Debug);
        }
        else
        {
            var a = new MyClass() { x = "stack" };
            var b = new MyClass() { x = "overflow" };
            a.other = b;
            b.other = a;
            this.ViewState["a"] = a;
            this.ViewState["b"] = b;
            MessageManager.Show("Init", MessageSeverity.Debug);
        }
    }
}

Now I get the messages “False” and “True” (in that order). Again, inspecting Viewstate shows, that each object got serialized twice. What gives? I checked the source of System.Web.UI.StateBag with ILSpy, but it just pushes all the values into an ArrayList, and that has no special serializing code either. So whoever is serializing the viewstate (System.Web.UI.ObjectStateFormatter?) is somehow taking each object and serializing as a separate graph… why??? And can I work around it?

Update: The reason I need this is that the same object will be persisted by two separate components, and upon deserialization I would like to check if they have the same object. (Or rather, they both store collections of objects themselves, and I need to synchronize those collections).

I could implement custom comparison in a dozen different ways, but since I want to do this for arbitrary objects, it kinda gets tricky. 😉

  • 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-22T12:13:09+00:00Added an answer on May 22, 2026 at 12:13 pm

    you could override the == operator for your class, or override Equals and do the comparison via a.Equals(b) to see if they are the same, rather than relying on the default equality based on address.

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

Sidebar

Related Questions

The short question, is there an off the self function to make a graph
Short question: Do any of MS's built in Data Objects support INotifyPropertyChanged? Long explination:
Short question - how do you define your view models? Here are some of
Short question: Is it possible to do a redirection, say when a user isn't
Short question: Is the parent of an entity group included in that entity group
Short question. How do you go about transposing UML diagrams into code? The class
A short question: I've got a TabNavigator with multiple canvas children. How can i
very short question. Is C++ typeof operator standard? de facto standard? which compilers do
This is a short question. At some point my thread understand that it should
I have a short question. Im my current project I'm using LINQ-to-SQl. That is

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.