Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: C# type definitions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 This is a little section that you are going to
0:02 be very excited about as a C# developer.
0:05 It's probably been a little funky for you to deal
0:08 with Python in its dynamic language types and sometimes
0:11 you're just like, oh, what is this?
0:13 What operations can I do on this element
0:15 or why can't I say this is an integer
0:17 or something like that.
0:19 We're going to look at the C# type system
0:21 super quick because I'm sure you know it really well
0:24 and then we're going to do something similar
0:26 in Python, surprisingly.
0:27 So we've got a class, it is a Wizard, like from a game
0:30 or something and I know normally you would put this
0:32 in its own wizard.cs file but with all this stuff going on
0:35 we're like running low on grouping options
0:37 so I'm putting it in this typing file here, okay?
0:40 It has a string, which is a name
0:42 it has a name which is a string, rather
0:43 it's a level, which is an integer
0:46 and it has this factory method called Train.
0:49 You can pass in a base level and out pops a wizard
0:52 that is trained up to that level.
0:55 Not a lot going on there, but whatever.
0:57 And then here's the main method.
0:58 We're going to create a wizard, we're going to call him Gandalf
1:01 and we're going to level him up a little bit and we're going to
1:03 say the level of the wizard is whatever their level
1:06 happens to be, okay? Run this.
1:10 Awesome, the level of the wizard is 8, and it started
1:12 out as 7 and obviously it compiles and runs.
1:16 So this is just a real simple case
1:18 of working with types, right?
1:20 We have a wizard class, we say dot, we get Train.
1:23 Obviously if we can explicitly state this is a wizard
1:29 and everything works, but if we tried to say it's an int
1:32 obviously not so much, right? Not so happy.
1:36 If we try to change this, obviously you can't just leave it
1:39 void or whatever, right?
1:41 We have to say either var, where it implicitly
1:43 adapts the type that is returned here
1:46 explicitly a wizard, or we have to say it's a wizard
1:48 like so, right?
1:49 And once we do that we have all the type options, name and
1:53 level and things like that that are a part of this class.
1:57 That's C# typing, and let's just run it one more time
2:00 to make sure I didn't break it, 'cause it compiled
2:02 while I was fiddling with it.
2:03 Great, Gandalf is level 8 in C#.