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.
Related
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.
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:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers
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.