UrbanPro

Learn .Net Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

What are Singleton classes in C sharp?

Asked by Last Modified  

15 Answers

Learn .Net

Follow 0
Answer

Please enter your answer

Computer Coder/Programmer

You are building an application in C#. You need a class that has only one instance, and you need to provide a global point of access to the instance. You want to be sure that your solution is efficient and that it takes advantage of the Microsoft .NET common language runtime features. You may also want...
read more
You are building an application in C#. You need a class that has only one instance, and you need to provide a global point of access to the instance. You want to be sure that your solution is efficient and that it takes advantage of the Microsoft .NET common language runtime features. You may also want to make sure that your solution is thread safe. read less
Comments

Trainer

A class where only one instance was maintained is called a singleton class. It is also called singleton pattern. You can make any class as a singleton class by introducing small steps like below. The constructor must be a private constructor and an static object for the class must be as a read only...
read more
A class where only one instance was maintained is called a singleton class. It is also called singleton pattern. You can make any class as a singleton class by introducing small steps like below. The constructor must be a private constructor and an static object for the class must be as a read only property exists in the class. See below code, FYI, public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton Instance { get { if (instance == null) { instance = new Singleton(); } return instance; } } } read less
Comments

B.E. Computer

A singleton class is which only allows a single instance of itself to be created, and usually gives access to that instance. Most commonly, singletons don't allow any parameters to be specified when creating the instance.
Comments

Computer Programming Languages

A system only needs to create one instance of a class, and that instance will be accessed throughout the program. Examples would include objects needed for logging, communication, database access, etc.
Comments

Software Developer with 10 + Years of experiencing in Coding and Training.

Singleton pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance and provides a global point of access to it.
Comments

Software Developer with 10 + Years of experiencing in Coding and Training.

eg:using System; class MainApp { static void Main() { // Constructor is protected -- cannot use new Singleton s1 = Singleton.Instance(); Singleton s2 = Singleton.Instance(); if (s1 == s2) { Console.WriteLine("Objects are the same instance"); ...
read more
eg:using System; class MainApp { static void Main() { // Constructor is protected -- cannot use new Singleton s1 = Singleton.Instance(); Singleton s2 = Singleton.Instance(); if (s1 == s2) { Console.WriteLine("Objects are the same instance"); } // Wait for user Console.Read(); } } // "Singleton" class Singleton { private static Singleton instance; // Note: Constructor is 'protected' protected Singleton() { } public static Singleton Instance() { // Use 'Lazy initialization' if (instance == null) { instance = new Singleton(); } return instance; } } } read less
Comments

Technical & Corporate Trainer

Singletons make having single instances easy. They allow for single allocations and instances of data. The singleton design pattern is an interface. It is a popular class type for programs. It allows a class to enforce that it is only allocated once. This is one of the best implementations of single...
Comments

Trainer

What I fail to understand is why do people not take a look at enhancements in a Programming Language before implementing the desired solution! A Singleton, as rightly explained by most of the people here, addresses the need for an Object of a Class to be created only once during the run of the application....
read more
What I fail to understand is why do people not take a look at enhancements in a Programming Language before implementing the desired solution! A Singleton, as rightly explained by most of the people here, addresses the need for an Object of a Class to be created only once during the run of the application. For example, the System.Console Class provides access to the Standard Input, Output and Error Device on the system. There can be only one such device per system - hence the need for it to be a Singleton. There are several such classes within the .NET Framework which are architected as Singleton classes. Another example is the ABOUT window of a typical desktop application – there should be only one About window open. Or for example, the File Open or File Print dialog window – there should be only one of these dialog boxes open, across all running instances of the application. So, the question is how does one create a Singleton class. It is one of the firstly taught/learnt Creational Design Pattern. As rightly stated by some of the contributors, the standard approach is to create a private constructor, and enforce an instance of the type is exposed to the outside world by a complex object creation method (following the Factory Method Pattern). But then, remember you are now working in .NET. The people who designed the Framework , while defining the standards that each and every language have to follow – while defining the Common Language Specification – have addressed this fundamental need in an extremely beautiful way. All you have to now do is define the class as a Static Class (which enforces that all members are now Static – meaning only one copy can exist) – and define whatever you need as Public members within the class. For example, System.Console class has been defined as: public static class System.Console { … public static void Beep() { … } public static void WriteLine(string) { … } …. } A static class is just another class – except that it enforces the policy that you cannot instantiate it at all. The class is loaded by the CLR’s Class Loader, the first time it is required, and remains in memory, until the application is unloaded. It cannot be disposed – and hence should be used only when absolutely necessitated. It is because of this very property that a Static Class is used to help define Extension Methods, since only one copy of the extended code can exist for all the objects of the extended type. read less
Comments

.NET Trainer And SQL Server Trainer

The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. Most commonly, singletons don't allow any parameters to be specified when...
read more
The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. Most commonly, singletons don't allow any parameters to be specified when creating the instance - as otherwise a second request for an instance but with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter, the factory pattern is more appropriate.) using System; public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton Instance { get { if (instance == null) { instance = new Singleton(); } return instance; } } } read less
Comments

Tutor

its one of architures impemented in advanced oops cocept
Comments

View 13 more Answers

Related Questions

why we are using MVC instead of ASP.Net
MVC is not replacement of ASP.net but it is used along with ASP.net. It is architectural pattern with no code behind, no RAD controls, no ViewState, no ASP.net page life cycle etc to reduce burden on server and improve performance of webforms.
Mallikarjuna
I completed my graduation in 2017, now working as an HR Executive in a Consultancy. I want to move to IT Sector. Which course is best for me to learn and get success in life? Please Suggest me
Dear Kumar, My suggestion is to - become good in one programming language - preferably Java and one O/S preferably Linux. Be aware of Open Source systems. Try to identify the opportunities in your existing...
Kumar
How will i get a NET tution?
Hello Ankita, I am .Net Trainer. Having more than 5 Years of Experience. I am Located in Bangalore. I will train for both Theory and Practical with the help of Real time Projects. You can spent few minutes and discuss with me regarding .Net Course.
Ankita
I want to do Project in MVC 6 with using WebAPI, Entity Framework and LINQ.
. I am working as a senior developer and I deliver same knowledge in my classes with project.I can help you in this.
Ashish
I did a basic programming course in my 12th Standard. Can I do .net ajax?
If you have good understanding of how web applications work and knowledge on html, javascript and any server language like php, C# or anything else then you can do .net ajax easily
Geeta

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

What are various validator in ASP.NET 2.0 and features common in all validation control in ASP.NET
Control to validate an error message. Are common feature in all the controls.Various validators are requiredfeild validator ,compare validator ,regular expression validator,custom validator,range validator...

Understanding Delegate in C# .NET
C# Delegate technique Delegate is one of the fantastic tool & technique in C#.NET. Using deleagte technique the following benefits could be avaialed One can define a method without name (Anonymous...

Be FOCUS !!
We must be very focused about learning & Goals. While learning please check your progress and take feedback at various level. Try Again & Again & Again !!! Deepak Garg

ASP .Net Interview Questions.
1. What is ASP? Active Server Pages (ASP), also known as Classic ASP, is a Microsoft's server-side technology, which helps in creating dynamic and user-friendly Web pages. It uses different scripting...

.NET FRAMEWORK
.NET FRAMEWORK IS A PLATFORM/AN ENVIRONMENT FOR THE DEVELOPMENT, DEPLOYMENT, AND EXECUTION OF HIGHLY DISTRIBUTED, COMPONENT-BASED APPLICATIONS. Objectives of .NET framework To support platform...

Recommended Articles

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Looking for .Net Training ?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for .Net Training Classes?

The best tutors for .Net Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn .Net Training with the Best Tutors

The best Tutors for .Net Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more