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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:24:43+00:00 2026-05-18T20:24:43+00:00

We have a custom DLL implemented IHttpModule to handle the httpApplication_EndRequest , what I

  • 0

We have a custom DLL implemented IHttpModule to handle the httpApplication_EndRequest, what I want to know is

The DLL has a class (not a static class) which has a static property is used to create an instance for a static variables/object reference defined inside the class.

Now, should I need to lock inside static property before creating an instance for the static object/variable?

Eg:-

public class SPEnvironment : IEnvironment
{
    private static SPEnvironment _instance;
    private static object _syncRoot = new object();

    private SPEnvironment()
    {
        try {
        .....
        }
        finally {
        ......
        }
    }

    public static SPEnvironment Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (_syncRoot)
                {
                    if (_instance == null)
                    {
                        _instance = new SPEnvironment();
                    }
                }
            }
            return _instance;
        }
    }
}

I will calling this from an another class, like below

SPEnvironment.Instance;

Is it the right way? or the lock should be removed?

  • 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-18T20:24:43+00:00Added an answer on May 18, 2026 at 8:24 pm

    The double null check with a lock in the middle is a good, thread-safe way of instantiating a singleton. However, you could save yourself a lot of code by just saying

    public class SPEnvironment : IEnvironment
    {
        public static SPEnvironment Instance = new SPEnvironment();
    
        private SPEnvironment()
        {
            try {
            .....
            }
            finally {
            ......
            }
        }
    }
    

    The difference between the two is that this code instantiates the singleton the first time an object of that type is created where your code instantiates the singleton the first time SPEnvironment.Instance is accessed. In almost all cases, those are the same thing; in most of the remaining cases, it doesn’t matter; but it is a subtle distinction that is worth understanding for that very rare edge case.

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

Sidebar

Related Questions

We have a custom DLL implemented IHttpModule to handle the httpApplication_EndRequest , what I
I have an executable project called A.EXE which references a C# class library B.DLL.
I have the following powershell script which loads a custom .NET DLL this contains
I have a simple C++ DLL that implements a few custom actions for a
I have a custom project type based on the C# Class Library project type
I have implemented my custom button inheriting from CButton and drawing it by using
I have added some custom actions to our installer which is run on CE5
I have a custom configuration section and i want to generate an xsd scheme
I have to call functions from a custom dll from BizTalk orchestrations (similar to
I have a c# application that is using a custom DLL (written in C,

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.