This question already has answers here:
C# Reflection: Is it possible to find an instance of an object at runtime?
(7 answers)
Closed 3 years ago.
I have a class implementing an interface. Is there a way to tell if that class has been instantiated (via AppDomain maybe) and then get a reference to the object via the known interface?
I suppose this is related to dependency injection. Rather than having a library with registered objects, I'm looking for an alternative.
You could set a static variable in the class that gets set when the constructor is called.
get a reference to the object via the known interface
This makes it sound like you only expect to have one instance of this class. If so, check out the Singleton pattern.
Related
This question already has answers here:
When should I use a struct rather than a class in C#?
(31 answers)
Closed 4 years ago.
Why POCO objects are created using classes instead of structs in C#, while POCO is intended to carry only public attributes and not any behaviors?
Do we really need to create a class with get and set accessors? I think structs would be much cleaner and simple to carry object's state.
In C#, struct type is a value, so looks that it is not a good choice.
This question already has answers here:
Static binding doesn't update when resource changes
(2 answers)
Static property using INotifyPropertyChanged. C#
(1 answer)
Closed 5 years ago.
I need help with a curious problem. I haven't managed to find anything similar on SO.
I have a static class used for logging in the system and now I have to bind the state of the log on the UI.
Is there a solution to this since implementing INotifyPropertyChanged forces one to use an instance member(which is an event in this case) and the class itself is static.
I would like to avoid retailoring the class to use singleton or any of those workarounds if it can be avoided.
This question already has answers here:
Use of var keyword in C#
(86 answers)
Closed 6 years ago.
If I use a IoC container and the strong implementation is instantiated at runtime, dunamically, which among the below initialization is a best practice?
var obj=FooIocContainer.Resolve<IInterface>();
or
IInterface obj=FooIoCContainer.Resolve<IInterface>();
Which one is better and why? Just being curious :)
It depends on the scope of the object you create. If its local then var will do. If you need to expose it using public properties, use specific Interface types.
This question already has answers here:
When to use static classes in C# [duplicate]
(11 answers)
Closed 8 years ago.
In C#, you can declare a class as static, which requires all members to also be static. Is there any advantage (e.g. performance) to be gained by doing so? Or is it only a matter of making sure you don't accidentally declare instance members in your class?
Or is it only a matter of making sure you don't accidentally declare instance members in your class?
It's that, but it's more than that:
It prevents instantiation by not having an instance constructor at all (whereas all other classes have a constructor - either one you declare or an implicit parameterless one)
It expresses your intention clearly
Nothing can derive from your class (it's both sealed and abstract)
Nothing can declare a field of your class type
Nothing can use your type as a generic type argument
Basically it tells the compiler and other developers "This is never meant to be instantiated - so if it looks like you're trying to use an instance, you're doing it wrong."
I doubt that there are any performance benefits, but all the above are enough for me :)
Oh, and you can only declare extension methods in static classes too...
I guess you are close to answer, sharing the link which might answer your question.
https://softwareengineering.stackexchange.com/questions/103914/why-and-when-should-i-make-a-class-static-what-is-the-purpose-of-static-key
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Try to describe polymorphism as easy as you can
What is polymorphism?
Please read MSDN which covers it in reference to c#,
Basically a derived class inherits from another class it gets all its methods,events and properties, and every type is polymorphic in .NET since they all have Object as their base class.