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.
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:
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:
How do arrays in C# partially implement IList<T>?
(6 answers)
Closed 8 years ago.
I have a class constructor that takes a IList<IElement> as an argument.
When creating a new instance of the class I'm able to pass a IElement[] instead of the IList<IElement> how is that posible?
An array with element type T derives from IList<T>.
This is not visible in the meta-data in mscorlib.dll, but the inheritance relationship is created at runtime in the CLR. C# and the CLR are aware of the array type and treat it specially.
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.
This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
typedef in C#?
STL like containter typedef shortcut?
I was wondering if there was an equivalent to Type aliasing from Functional Languages that I can use in C#
For example in a nice functional language like Haskell I can say something like the following to alias an existing type to a custom Type Name
type MyCustomTypeName = String
I'd like to do this as I have an API that I'm building where some of the Objects I'm using have multiple possible names in the sense they could be referred to by several terms which are interchangeable and equivalent. Presumably I could do this with inheritance but that seems somewhat clunky and then potentially breaks if people start extending the non-canonical class ie.
public class CanonicalClass {
//Full Implementation
}
public class AlternateName : CanonicalClass {
//Empty except I'll need to redefine all the constructors
//Could declare it sealed but doesn't get rid of the need to redefine constructors
}
And before anyone mentions interfaces all the Classes in question are all implementing interfaces already and there are multiple differing implementations of these interfaces.
Depending on what you're actually trying to do (give a somewhat more complete example), you may indeed need interfaces (used properly) and/or generics.