Join Bruce Van Horn for an in-depth discussion in this video Understanding C# syntax, part of C# Essential Training: 1 Syntax and Object Oriented Programming.
- [Instructor] The C# syntax is said to be C like in the way it structures its statements. Like Java, it's a strictly object oriented language, so all your code will contained within class files. In the project on the screen, you're looking at a simple program that outputs a message to the Windows console. The code here is contained in a file called program.cs, which matches the name of the class on line six. Most class files contain statements at the top which define dependencies for the class. It's very common to use dependencies from within the .NET Framework as well as from third party vendors.
In the example on screen, we can see a using statement declaring a dependency on the System library, which we'll need in just a minute when we output a console message to the console window. Classes in C# are organized into namespaces. A namespace allows you to organize your code in order to avoid class name collisions. For example, I might need to have two classes within the same project that have the same name. I can segregate them by putting them into two different namespaces. This prevents the names from being ambiguous, which would prevent my code from running.
After the namespace declaration, you'll see the class definition itself here on line six. Here we're looking at a class called Program. The class keyword defines the class, and it's possible to have multiple classes in one file, though that practice is generally frowned upon. Code blocks in C# are delineated using curly braces. You can see one here at the top of the namespace on line five, and you can see a second one here for the class on line seven. You'll also see that those are closed on lines 19 and 20 respectively.
Inside the class you'll find the properties and the methods. The properties hold data. For example, on line eight I've got a field that contains a simple text string that says C# Essential Training, and on line nine I've declared a field that contains an integer or a whole number called whichPart. Down inside the main method on line 11 you can see that I'm actually assigning a value on line 13 to the field whichPart, and then I'm using title and whichPart to construct a string message on line 14.
On line 15 I'm outputting that to the console. You'll note that all statements in C# are terminated with semicolon. You'll also note that in Visual Studio, keywords reserved to the C# language are color coded differently than the variable names and function names that I've used to define my class.
- The history of C#
- Setting up your development environment in Visual Studio
- Declaring variables
- Working with strings
- Using mathematical operations
- Creating classes and properties
- Using expressions
- Using static, abstract, and virtual methods
- Building a user interface
- Extending classes
- Working with subclassed objects
- Using the object-oriented features of C#