preface

Remind: In order to be able to learn the knowledge more thoroughly, remember more firmly I will write down the knowledge by means of teaching on Because can let a person in the process from students to the teacher This process will be dug up new knowledge and ideas is a self thinking mining switch and a depth of knowledge and the process of ascension If you can help to everyone that is the best If there is a mistake, please give me more advice! I’m just a vegetable chicken thanks for understanding!


1, Hello World program

Before I get into programming languages I want to remind you that one of the worst things about learning a programming language is reading it without writing it

While watching the brain: Mmm, that’s easy. I can do it! Hand: No you won’t! What are they when you write them

So we’re going to have to get our hands on it and without further ado we’re going to open up the Vistudio Studio compiler or instead of VS you can just use whatever you’re comfortable with and create a new console application project

Let’s start with a simple piece of code

Let’s look at the execution results first

Output Hello World!

using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!" ); Console.ReadKey(); }}}

Let’s look at a simple C# program structure

  • Using System: References the namespace named System
  • Namespace ConsoleApp1: Declare ConsoleApp1 namespace declaration allows us to refer to ConsoleApp1 using
  • Class Program: Declares a Program class
  • Main: The entry point of the program
  • Console.writeLine () : Output information to the screen (which is our Console window). We only see Hello World in the Console window. So that’s what this does and we can modify it to see what it looks like so we can change the statement that we want to print

    Console.WriteLine(” I’m just a Unity chicken “);

The console window will see the output saying “I’m just a Unity chicken.

  • Console.readKey () : Wait for the user to press any key. To avoid a flash when the program is compiled, delete this and see what happens
  • If we look closely, we can see that all statements and expressions in this code must begin with; A semicolon

I always have one at the end of this statement; A semicolon

Console.WriteLine(" I'm just a Unity chicken ");

This is because the language requires a semicolon to indicate the end of a statement. All programs must get into the habit of ending a statement with a semicolon otherwise the program will report an error


2. What is an identifier

Identifiers are general terms used for variables, classes, methods, and various other user-defined objects.

int index = 0;

Index is an identifier

In short, the name we assign to a variable or user-defined type is called an identifier

Naming identifiers is a science that requires us to follow certain rules

Such as:

  • Must begin with a letter or _
  • You can’t include Spaces
  • It can contain upper and lower case letters, numbers, underscores, or @ characters
  • Identifiers must be case sensitive, and uppercase and lowercase letters are considered to be different letters
  • The @ character can only be placed first in an identifier. Identifiers with the @ prefix are called verbatim identifiers
  • Identifiers cannot use the keywords in C#, but adding the @ keyword can be a valid identifier but is not recommended
  • Cannot have the same class library name as C#
    • *

3. What are keywords

We can see in this simple program that using, namespace, class, and all of these are C# predefined reserved identifiers. These reserved identifiers have special meaning for the compiler and we cannot use them as identifiers for our program (identifiers cannot be repeated with keywords unless at sign is added).

So let’s say we define an int variable called using and then our program will report an error

Since a using is a predefined reserved identifier that helps us refer to other namespaces, we can’t use it as an identifier for our own variables, right

int @using = 1;

Just put the @ sign on

C# to provide a lot of keywords when the beginner to understand the next good do not need to remember because a lot of keywords behind will be frequently used over time can remember the attached C# official provided by the C# keyword document will not be checked

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/


4. Why refer to namespaces

Here we take another look at the concept of reference namespaces

The reason we were able to call console.writeLine (); This method (or function) is used because we refer to the System namespace, and because the method console.writeLine () is in the using System namespace, what happens if we don’t refer to the System namespace

Let’s drop the phrase using System

namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!" ); Console.ReadKey(); }}}

You can see that the compiler is reporting an error!

Prompt that there is no Console in the current context and refer back to System

using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!" ); Console.ReadKey(); }}}

Compiling is working again

Let’s take a closer look at the reason we were able to call console.writeLine (); This method (or function) is used because we refer to the System namespace, and because the console.writeLine () method is in the using System namespace

Click Console and press F12 or right click to go to Definition and let’s see

You can observe that there is a Console class defined in the System namespace

Then we scroll down or go back to our program and click on WriteLine and then F12

See writeLine ()

So there you have it, console. writeLine () in the System namespace Console. if we don’t refer to the System namespace we can’t call it. It’s like in real life you have to go over to your friend’s house and borrow his PS4 and you don’t know where he is So how do you get him to borrow it