Object Lifetime

JasinCole

Well-known member
Joined
Feb 16, 2023
Messages
66
Programming Experience
1-3
Simple question here, but in the following class code a property that creates and returns an instance of itself.


C#:
public class NavigationService : INavigationService
{
    public static NavigationService Instance { get; } = new NavigationService();

    public void SetFrame(Frame frame)
    {
        _frame = frame;
    }

    public void Navigate(Type type)
    {
        _frame.Navigate(type);
    }

    private Frame _frame;
}

What is the object lifetime?, if the calling code is
C#:
_frameView = this.FindControl<Frame>("RootFrame");

NavigationService.Instance.SetFrame(_frameView);

I assume that since NavigationService field _frame references an object lifetime that exist for the entire program that NavigationService object will have the same lifetime and it essentially makes this a singleton?
 
in the following class code a property that creates and returns an instance of itself

It's not how i'd describe it. That's a static property that is initialized once by the runtime to a new instance, and then the property returns that instance. Getting the property does not create a new instance and return it

NavigationService field _frame references an object lifetime that exist for the entire program that NavigationService object will have the same lifetime and it essentially makes this a singleton?

When you say "this" if you mean the Frame, then I disgree that it's a singleton. I don't see any code that makes it impossible to create another NS, nor any code that inhibits a Frame to being a single instnace either, and there are methods that can be called on an NS to set a Frame to a new instance, an existing instance or a null. None of this fits with what I consider a singleton, but you could use it in singleton fashion
 
Back
Top Bottom