Dynamic Vs Var in C#

preview_player
Показать описание

Want to see a full C# application built from scratch?

There is a lot of confusion around what the dynamic and var types are and when to use them. This video clears that up. Whether you need to know it for a job interview or just to use them properly, this video will show you what they are, why they exist, and when to use them.

0:00 - Intro
0:40 - basic difference explained
3:51 - dynamic auto type conversion and why not to use it
7:07 - examples of how dynamic may fail you at development and fail your application
15:05 - method return type
17:01 - why and when: dynamic
19:02 - why and when: var
24:57 - var as anonymous object and summary
Рекомендации по теме
Комментарии
Автор

dark mode, clear voice, amazing explanations. thank you tim corey!

nyuin
Автор

I've never even heard of the dynamic keyword until this video.

lukenukem
Автор

0:40 - basic difference explained
3:51 - dynamic auto type conversion and why not to use it
7:07 - examples of how dynamic may fail you at development and fail your application
15:05 - method return type
17:01 - why and when: dynamic
19:02 - why and when: var
24:57 - var as anonymous object and summary

RalfsBalodis
Автор

If for nothing else, I think the dynamic keyword is useful as an educational tool to compare and understand the differences of static and dynamic typing, side by side in the same language. Otherwise, whenever one wants to discuss the differences between the two typing approaches, one has to compare different languages where they are applied, e.g. Java vs JavaScript or Python, which may easily lead to some sort of apples and oranges comparison - not to mention heated debates between the proponents of those languages.

michaelschneider
Автор

I've been developing for several years and have never ran into dynamic before. Great video!

PanikGrafik
Автор

Thank you for the explanation, I'm learning C# and I had a lot of doubts about the difference in this two keywords, and now I know exactly how to use them, thanks for yours tutorials!

carlosmarte
Автор

Thank you very much for the effort you put into your videos, the quality, depth and ease of grasping the concepts taught is top notch. You are a great teacher indeed.

throxe
Автор

The most well thought out and useful videos on development with C#! Love the content!

AmandeepSingh-xkyv
Автор

I think (with limited knowledge and experience) that using var seems to be the most clear and concise way of initializing a variable in C#. With a good name and a peek to the right of the assignment statement you should know exactly what you're dealing with.

josephmills
Автор

Hi Tim. Thanks for your videos. I am an experienced developer with several languages under my belt, but mostly C++. I have very recently decided to bring C# into my repertoire. Near the end of this video you talked about using var vs the actual type and said that you preferred to use the actual type most of the time. The reason that you gave for people wanting to use var all the time was because variable names must be descriptive and so the type is easy to determine from the name. I don't know if that is the main reason for most people in that camp, but it's a bit bogus.

Like you I use both. I however prefer using var more often rather than less. It seems that var in C# is the same animal as auto in C++. Here are a few reasons that I prefer to use it:

1. Using var forces you to instantiate you object variables. After all these years I still forget sometimes to add the 'new ...'. If the type were var then this problem is caught easily by the compiler and you won't be subjected to weird behaviour or crashes from this. For this reason I would definitely use it even for your Person class.
2. Using var forces you to initialize all your variables, including basic types. This is very helpful in avoiding weird errors due to unexpected values. There is no harm in casting your initializer value to make it's type explicit.
3. In a foreach or similar construct the data type of the container or even the container's content could change after further development. Using a var to refer to it means that in this scenario your code will be less likely to break in subtle ways such as you now pointing to the wrong type because you had forgotten to change the type in some of the loops that you use it. The only places you might need to fix are where the interface of the object has changed slightly and that's usually a compile time error.

Benefits as I see it:
- Whenever you have to allocate an object you are forced to type in the data type so you know what it is just by looking at it so readability is satisfied.
- You can not go on without initializing it so both instantiation and initialization are satisfied.
- Code maintenance is made a bit easier.

That said, sometimes it is just easier to use a basic type, especially in very local scope.

hvbsalmarm
Автор

Great video! Now I clearly understand the difference between 'var' and 'dynamic'. Thank you!

alinapostol
Автор

Agree with your preferences on this. Our code is compiled because it's meant to be human readable and therefore unambiguous - explicit declaration facilitates this. Also as you've noted before, it makes you question if the types you're using are actually appropriate: critical thinking can only be a good thing.

simon-white
Автор

Thanks a lot Tim, it really clarified my doubts and usage of Dynamic vs Var. Really helpful.

subashbarik
Автор

As I have come to expect from you, this is another great video. I was not even aware of the dynamic type before seeing this video but completely understand how it is different from var. I am new to programming (knowing only C# at this point) but tend to agree with you on mixing var and explicitly stating the type for simple variables. All the best!

stevenchilders
Автор

Very helpful video and clear explanation, just wish you had included the 'object' type as well

afonsomarinho
Автор

Thank you so much for great and deep explanations of basic things, Mr. Corey! It is absolutely essential to understand those to make something way more complex.

torrvic
Автор

Ty so much for the explanations. So much easier to understand with audio and code vs just trying to follow some online written example on how to use each keyword. Totally makes sense to me now.

robbiexiong
Автор

Thanks Tim. Your presentation are really resourceful for those interested in knowing it all. Please keep it up!

ambroselangat
Автор

Thanks a lot for your videos!!!
You deserve the thumb up even before I watch your videos 😊

zlatkozeco
Автор

We can say that C#'s var keyword is analogous to C++'s auto keyword? One good use case I can think of for var is if we have a type name that's hard to type out because it requires chaining quite a few nested namespaces and then a class name or something. Dynamic is best for working on a system requiring communication with different systems? At that point, one could even just return a JSON object (if possible) and parse that using C#'s Newtonsoft.JSON library to a custom defined C# class instance (or an anonymous var). The only other use I can think of for dynamic is that it's a bit tricky to pull individual properties from an object type, and dynamic is a bit easier to work with in that regard.

arturoordonez-hernandez