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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:29:49+00:00 2026-05-26T23:29:49+00:00

I need to share an instance of an object between two static objects as

  • 0

I need to share an instance of an object between two static objects as described beneath. The first time I call the property MyProperty I must instantiate MyObject.

As a clever reader, you’ve found the bug. The first time I’ll use the First, I’ll instantiate MyProperty and the first time I’ll use Second, I’ll instantiate MyProperty. That’s reinitialise it and loose all the state of this object.

public static class First
{
    static First() { MyProperty = new MyObject(); }
    public static MyProperty{ get; set; }
}

public static class Second
{
    static Second() { MyProperty = new MyObject(); }
    public static MyProperty
    {
        get { return First.MyProperty; }
        set { First.MyProperty= value; }
    }
}

The solution in this case is just to check nullity:

static Second()
{
    if(MyProperty == null) MyProperty = new MyObject();
}

But, for me there’s a smell. I’ve got the feeling of bad code. So there’s my question: is there a pattern or some good advices to share a context between two static objects?

  • 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-26T23:29:50+00:00Added an answer on May 26, 2026 at 11:29 pm

    You do not need the Second constructor, since First.MyProperty is initialized by First as soon as you access it, no matter whether through Second or “directly”. However, as Daniel comments, you might consider using instances. Stateful statics are a smell too.

    As an alternative, you may consider creation a Factory whose constructor takes a MyObject. This factory provides two methods for building First and Second instances, to which MyObject is passed. This does require you to pass around the instances instead of accessing the static classes, which I must admit is less convenient.

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

Sidebar

Related Questions

I need to share SSO information between two different domains with a cookie, can
I need to share a stack of strings between processes (possibly more complex objects
Is it possible to share Objects between C# applications without serialization? I need three
What is a good way to share an instance of an object between several
I use singletons often when I need to have a shared instance object across
Is there a common way to share a lock between different objects operating on
I would like to share data between the objects. Actually, for example, there is
I have 2 Java EE applications which need to share some common user related
I have Client-Server environment and developed a project for Client-Server. I need to share
I need to create several applications that all share a Microsoft SQL Server database.

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.