C# OOP: Static Instance Explained

  • Context: C# 
  • Thread starter Thread starter FrankJ777
  • Start date Start date
  • Tags Tags
    Oop Static
Click For Summary
SUMMARY

The discussion focuses on the implications of declaring a Queue instance as static in C#. A static instance, such as public static Queue messageBuffer = new Queue();, behaves as a singleton, allowing it to be accessed without instantiating the containing class. In contrast, a non-static instance, public Queue messageBuffer = new Queue();, requires an instance of the class to be accessed. The static keyword ensures that there is only one instance of the Queue throughout the application, which can lead to shared state across different parts of the program.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with object-oriented programming concepts
  • Knowledge of static vs instance members in C#
  • Basic understanding of collections in C#, specifically the Queue class
NEXT STEPS
  • Explore C# static classes and their use cases
  • Learn about the Singleton design pattern in C#
  • Investigate the behavior of static fields in multithreaded environments
  • Review C# collection classes and their performance implications
USEFUL FOR

C# developers, software engineers, and anyone looking to deepen their understanding of object-oriented programming and static members in C#.

FrankJ777
Messages
140
Reaction score
6
I have a question about instatiating a class as static and what that implies. Going through an old project of mine I noticed I did the following:

public static Queue messageBuffer = new Queue();

how does that static instance of the Queue behave compared to one declared without the static modifier? For example:

public Queue messageBuffer = new Queue();

Does it make a difference. I've played with both instances but can't tell the difference, but i want to make sure I know what I"m doing.

Thanks
 
Technology news on Phys.org
FrankJ777 said:
I have a question about instatiating a class as static and what that implies. Going through an old project of mine I noticed I did the following:

public static Queue messageBuffer = new Queue();

how does that static instance of the Queue behave compared to one declared without the static modifier? For example:

public Queue messageBuffer = new Queue();

Does it make a difference. I've played with both instances but can't tell the difference, but i want to make sure I know what I"m doing.

Thanks

Have you modified messageBuffer (both static and other) data (attributes) and checked what happens to data when you create second instance that contains this object?

And, I would also suggest that some kind of output whenever Queue constructor is called (like Console.WriteLine("Queue is created") inside the Queue constructor.).
 
The static keywords turns the field into a singleton, this also makes it possible to access it without creating the containing class by using Classname.messageBuffer.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
3
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K