From the course: C# Essential Training 1: Types and Control Flow
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Defining constructors - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
Defining constructors
- [Instructor] With our types to find, we can start thinking about, how other developers or ourselves, are going to construct instances of these types. So we can start defining constructors for our classes. Go into my Employee class here, we use the little keyboard shortcut, typing ctor and Tab twice. Now I have a public Employee, then I'll just add two parameters here. String firstName, string lastName, and then inside of the constructor and set the properties. So LastName equals lastName, and FirstName equals firstName. And of course confirmation, that C-sharp is case sensitive. We've got a property name, it's different from our parameter name, simply by the first letter. And that now allows me to create an employee, passing in the firstName and lastName, and initializing the object with that state. I may have caught down here on line 28 though, that my Manager class, which derives from Employee is now giving me a red squiggly, which will turn into a compiler error, that says…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Understanding classes, structs, and records7m
-
(Locked)
Defining constructors7m 6s
-
(Locked)
Object initialization6m 4s
-
(Locked)
Initialize only properties4m 59s
-
(Locked)
Cloning and copying objects9m 32s
-
(Locked)
Equality comparisons5m 27s
-
(Locked)
Defining abstract classes7m 52s
-
(Locked)
Creating static classes5m 18s
-
(Locked)
Solution: Expressing equality1m 5s
-
-
-
-
-
-