var and dynamic Keyword in C# , related to Polymorphism? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Can we say var keyword is Compile time Polymorphism and Dynamic is Run Time Polymorphism?
var a=10; (Compile time Polymorphism)
dynamic a=10; (Dynamic is Run Time Polymorphism)
a="XYZ"; (Dynamic is Run Time Polymorphism)

I don't think you can link var and dynamic with polymorphism exactly. Polymorphism is about actions and behaviours and not data type or bindings.
var is evaluated at compile-time and dynamic is evaluated at runtime. You are correct there though.
for a better understanding look into What's the difference between dynamic (C# 4) and var?
Also, a suggestion. A little bit of reading and searching would have answered your query.
More Description
Polymorphism is about an entity that can take many forms or behave differently. As an object can be a parent class instance or a child class instance. Now var cannot take multiple types on its own. Its type is just inferred with the type of object being assigned to it. So technically var is not compile-time polymorphism. Rather it depends on the object being assigned to that.
So, var and dynamic might help you achieve or demonstrate polymorphism, but they do not themselves are examples of polymorphism.

var and dynamic have no relation whatsoever to polymorphism. Polymorphism means that you can provide an instance of a more-derived type when a less-derived type is required. var and dynamic are just syntactic sugar; the compiler does the work of figuring out what type you mean or need given the situation (in the case of dynamic). Apples and oranges.

Related

why microsoft allowed list<object> = new list<object>() feature from 2.0? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been learning collection and generics and understood that generics were introduced to
provide more type safe containers and to improve the application performance by reducing number of boxing/unboxing.
But, If that was the case, why does the framework allows to create generics collection of type system.objects which can take any other type?
List<object> listObj = new List<object>();
listObj.Add(new Program1());
listObj.Add(new Program2());
Thanks in advance.
Yes the collection is List<T> is object not a type? object is a type like any other (being the base type doesn't make it any different) and therefor can be used as the type argument for generic collections, there is nothing wrong with that, just don't do it when you know what the specific type is.
I would say: why not. A list of objects, in which form you want it is totally legit. I personally don't want the framework to tell me what to do. It is fine to do suggestions, but prevent this code? No.
If you want a list of things that are neither the same type, nor share a base class, nor share an interface, then you need List<Object>.
You very rarely need this.
But if you pursue the thought that List<Object> should be disallowed because it encourages unnecessary and dangerous downcasts, and anyway with generics you can just specify the types actually can handle, shouldn't you then go all the way and disallow the use of Object entirely?

Are there applications for generic classes other than for collections? (Updated !) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Original Questions: I know the question sounds pretty "thin", since generic classes (interfaces) and collections go hand in hand. Out of curiosity and a desire to 'cover all the bases' ... are there uses for these generics other than as collections?
The response is that there are too many possibilities to make for a good thread, so let me try to clarify the question because I ( and probably others) will definitely benefit.
My revised question is:
What are applications of instantiated generics (not methods!) in addition to collections? So, now I know there are many ... however, classified by use... what are they?
A concise format for answers is:
Use: Short description or example
(ie) Collections: The generic allows for collections of objects and with a where T: constraint gives access to methods on all members of the collection. (link or reference).
I'm really eager to hear responses.
You can create not only generic types but also generic methods. Though the most common use of generics is for creating collections they are also used for many other purposes such as containers or algorithms.
class Point<T>
{
T x;
T y;
};
class Math<T>
{
T Add(T a, T b);
};
You should also have a look at this discussion: What is cool about generics, why use them?.
I've used generics for a "EventHandler" (with a restriction on the generic that the parameter implemented my BaseEvent class) when sending events via WCF to another piece of the system.
As the comments note, the answer is unequivocally yes. You use generics whenever multiple types (and ideally all types) should have the same behavior (and occasionally state). Collections are an easy example of this, but there are many, many other situations where this holds true and generics are a good choice.

Generate Runtime type based on propertyinfo[] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have got a list of PropertyInfo, now i need to either populate a new object with these propertyinfos and there values or generic a type runtime containing these properties so i can create a new instance of the object based on my runtime created type.
I cannot create a new instance of an object based on an earlier type because i just filtered out my collection based properties (this is due to serialization of an object; long story).
How can i achieve this?
I think TypeBuilder will suit your needs.
On the bottom of the page you will see a clear example.
Also if you need to build methods use Expression Trees instead of ILGenerator.
Here's a few options:
Use TypeBuilder to create a runtime type. Use DefineMethodOverride to implement the get/set methods of the properties and return the interface type (with the runtime implementation).
Use one of the many Mock frameworks. They basically do the plumbing for you; the result is the same.

Why anonymous types aren't dynamic as the ExpandoObject? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
With dynamic we pretty much have a dynamic pointer, but not exactly a dynamic object. The true dynamic object in C# is the ExpandoObject, but that is a really unknown class for most of people. The expando allows creating and removing members at runtime, much like a hash (similar to JavaScript).
Why the ExpandoObject goodness was implemented in a separate class rather than just being, let's say, implemented as a feature of anonymous types?
Maybe that wouldn't be a good move because the lacking of type-safety? Or maybe due the (DLR) overhead involved?
Because anonymous types have other very important feature - they provide you compile time type safety.
And because dynamic and anonymous types are just different concepts. The first one gives you ability to dispatch object members at runtime, the second lets you create statically typed objects with some base functionality (equality, hashcode, etc) without creating corresponding POCO classes. Why should they be implemented in the same way then?
btw. I use them quite a lot and really rarely needed to use dynamic to deal with them. Are you sure you're using these language features correctly?
Update
I think that's very important part of anonymous types tutorial:
If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

Some specific types in generic function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to make a a function, and i dont want to write it many times with different types.
Can I use 'where' and "tell it" that I want one of the specific types that I write?
By the way, I need a function to handle integers and another to handle floating numbers.
No, you can't. There are specific constraints you can apply for type parameters, e.g. it must be a non-nullable value type, or it must be a class, or it must implement an interface or whatever... but you can't specify a set of types and say that it must be one of those.
Even if you could do so, I suspect it wouldn't do what you want - because I expect you want to perform arithmetic on these types.
Two options:
If you're using C# 4 you could use dynamic typing. It doesn't give you compile-time safety, but it'll work if you're careful.
You could use Marc Gravell's generic operators in MiscUtil
You can't statically restrict the function to only take floats or integers. To do so, you would have to check the argument types manually inside the function.

Categories