C# and .NET Basics

CSharp Syntax Overview

Spencer Schneidenbach

Spencer Schneidenbach

Aviron Software, Microsoft MVP
C# and .NET Basics

Check out a free preview of the full C# and .NET Basics course

The "CSharp Syntax Overview" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:

Spencer discusses the basics of C Sharp syntax. He explains the use of curly braces to denote blocks of code and the importance of using semicolons to end statements. He also mentions the use of comments and the case sensitivity of C Sharp language. Spencer recommends referring to the Microsoft documentation for more detailed information on C Sharp syntax.

Preview
Close

Transcript from the "CSharp Syntax Overview" Lesson

[00:00:00]
>> Spencer Schneidenbach: So let's talk about CSharp Syntax just a little bit. We're gonna touch on the very basics, just a couple of things that we need to know as we get in. First of all, because it's a C-derived language, it loves its curly braces and it loves its semicolons.

[00:00:14]
I know in JavaScript, semicolons are technically optional, I like semicolons and I like that C-sharp makes me use them. So all of these, well, let's take a look at this particular program here. Starting at the top this is a class declaration. Again, as we get into the co further into the course, we will talk a lot about classes and we'll talk about their place in the .net kinda world, in the Csharp world.

[00:00:38]
And you notice that this class is denoted by curly braces here. So these curly braces denote that this is a block of code. So this is all of the stuff that's in internal class program. If we were to say, for example, copy and paste this method, it's gonna complain and hey, there's already a method named main that has the, these arguments.

[00:00:59]
Again, we'll get into method arguments a little bit later. But just know the main thing is, is that curly braces are everywhere in Csharp. And I like curly braces I like the explicit kind of, this is the block of code that we should execute based on this statement.

[00:01:13]
Of course, we have to talk about semicolons. Semicolons are denote the end of all of the lines that have statements on them that, frankly, require them. So if you do exclude a semi colon, for instance, your IDE will happily tell you with a little squiggly that, hey, this is expected.

[00:01:31]
So we're actually gonna look here and we'll see that if you if there's more to this syntax error, that Visual Studio code will happily report that to you, and so will the.net tooling. If I go ahead and command shift tilde and try .net run, you'll see that the same errors are outputted here right here semi-colon expected.

[00:01:51]
So a lot of statements have to end in semicolon. Things like method declarations they don't, program they don't. That's just going to confuse it. But the stuff inside the Blocks that's actually doing the work, we need some semicolons. Worth noting, you can also view problems here inside of the IDE.

[00:02:09]
I will say that there have been a couple of times as I've gone in my Visual Studio Code journey that if the problem doesn't show up here, a restart of Visual Studio Code usually kicks it into gear. Worth noting.
>> Student 1: Why does internal not have to be public or private?

[00:02:25]
>> Spencer Schneidenbach: That is a great question. And we will talk about accessibility modifiers as we get into the course, especially as we talk about classes. It's just the I'm not sure why it has to be internal. I assume that private is too private, as it were, elements defined in a name space cannot be explicitly declared as private or okay.

[00:02:43]
I'll take its word for it I've honestly never tried that. I think that this is the minimum amount of visibility required for .net to actually run the program, but we will talk about private internal protected. We'll talk about all those accessibility keywords as we talk about specifically classes more, because those become really important to talk about there.

[00:03:02]
We're gonna talk about kind of the styling. Let's say, I guess values that C sharp holds, you can absolutely declare your methods like this, if you prefer, you could even I don't think you can lowercase main, because I think it will complain that it says it doesn't have an entry point.

[00:03:18]
I've never tried so, let's see. Dotnet run yeah, it doesn't contain a static main method, but you'll see things. We will talk about kind of the, way things are named and typed. Microsoft has a specific style guide. If you don't like this on a separate line, you absolutely don't have to do that.

[00:03:37]
And in fact, you can configure most of your IDs to avoid that if you prefer, it's one of those stylistic decisions that's just personal and private, I prefer the C sharp way, but or whatever ecosystem I'm working in. But you do, you
>> Spencer Schneidenbach: I do wanna mention that while the language is not white space sensitive.

[00:03:59]
This is a perfectly valid program that will run, nope, keeps doing that dotnet run. Perfectly valid program white space is encouraged, useful. White space is encouraged, just for readability purposes, like any other language, but it is not strictly speaking, white space sensitive.
>> Spencer Schneidenbach: Some other things that I wanted to mention.

[00:04:25]
This is a single line comment. So this is basically a way of commenting out, what we call commenting out code. So this code will not be run or interpreted by the compiler. It's useful for leaving notes, especially in particularly tricky pieces of code. There are multi-line comments as well, like any good C derived language.

[00:04:45]
It is /asterisk, asterisk/. You can see, so you can have comments on multiple lines as well.
>> Spencer Schneidenbach: So we went over a few of the common syntax errors. It will complain, of course, if you don't have semi colons, you see that I don't have this unclosed comment. So now the IDE is hey, where are my other curly braces?

[00:05:09]
It will tell you if you have a an unmatched curly brace when you're especially when you're refactoring those big nested statements, and you have a lot of curly braces there, and all of a sudden, you're missing one very frustrating. But that's a particular thing that's a common one, especially among new developers.

[00:05:26]
It's worth mentioning as well that case does matter. It is a case sensitive, and I think most languages are case sensitive. And fun fact, vb.net, is not, and it is a point of frustration in many ways as a as a C sharp developer. I really don't know how that, I don't know why that is, but vb.net, not case sensitive.

[00:05:44]
Very strange, but you can't write cons and lowercase can't write cons and lowercase because it will complain. Boom but you can, it also knows the c dev.kit is intelligent enough to be able to kind of suggest sometimes some fixes for that. Some common ones, next, we will talk about variable declaration and kinda talk about all the little pieces, one little piece at a time as they say.

[00:06:11]
>> Student 1: There was somebody asking, is there a MDN like reference for C sharp? Something that's easy to like look up?
>> Spencer Schneidenbach: Yes, I would say that the Microsoft Docs, I'm just gonna Google Microsoft Docs C Sharp and I'm gonna just open these up and just say that I think that their documentation is probably among the best among their peers, Google, Amazon.

[00:06:37]
I think their documentation documentation is super solid. A lot of it contributed by the community. A lot of this documentation was written by the community. They also employ people to just sit and write docs all day that's all they do. And so absolutely this is learn.microsoft.com. I'm used to docs.microsoft.com as well.

[00:06:53]
But yeah, there's a huge swath of documentation and we'll go into way more detail than I could in a course like this. That said there are some things where I'm I'll mention it and then I'll mention the feature and then I'll link back to a Microsoft doc. Like, you really should read these because their docs are supremely good again some of the best in the industry, in my opinion.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now