当前位置:网站首页>Hello wrold

Hello wrold

2021-01-24 10:13:54 NewIDream

Preface

remind : In order to learn the knowledge more thoroughly 、 Remember more firmly I will write down the knowledge through teaching explanation Because in the process, people will change from students to teachers This process will dig out new knowledge and ideas It's a process of deep knowledge mining and upgrading achieved by self thinking switching If it can help you, it's best If there are mistakes, please give me more advice ! I'm just a vegetable chicken Thank you for understanding !


1、Hello World Program

Before learning programming languages, I want to remind you One of the most taboos in learning programming languages is Only read but not write

When you look at it The brain : Well, it's too simple I get it ! hand : No You can't? ! I'll find out when I write What are they all about

So we must start to write I don't say much nonsense We turn on vistudio studio compiler Or not VS Just use whatever you like Create a new console application project

Let's start with a simple piece of code

Engelbart

Let's take a look at the implementation results first

Engelbart

Output Hello World!

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


Let's take a look at a simple C# Program structure

  • using System : The quoted name is System The namespace of
  • namespace ConsoleApp1 : Statement ConsoleApp1 Namespace After the statement, we can pass using To quote
  • class Program : Make a statement Program class
  • Main : The entry point of the program
  • Console.WriteLine() : Output information to the screen ( It's our console window ) So we just see it in the console window Hello World! That's what this sentence does We can modify it to see the effect Let's change the statement we want to output

    Console.WriteLine(" I'm just a Unity Chicken with vegetables ");

You can see the output in the console window I'm just a Unity Chicken with vegetables

  • Console.ReadKey() : Wait for the user to press any key In order to avoid the flash after compiling the program You can delete this sentence and see what's changed
  • Careful look at We can find that all the statements and expressions in this code should be written with ; End of semicolon

Like this sentence There's always one at the end ; A semicolon

Console.WriteLine(" I'm just a Unity Chicken with vegetables ");

This is because the language prescribes Statements need a semicolon to indicate the end of the statement All programs must form the habit of ending with semicolons Otherwise, the program will report an error


2、 What is an identifier

Identifiers are used in variables 、 class 、 Method 、 And various other general terms for user-defined objects .

int index = 0;

index It's an identifier

To put it simply The name we assign to a variable or user-defined type is called an identifier

Naming identifiers is a science It needs us to follow some rules

for example :

  • Must be written in letters or _ start
  • Cannot contain spaces
  • Can contain upper and lower case letters 、 Numbers 、 Underline or @ character
  • Identifiers must be case sensitive , Uppercase and lowercase letters are recognized as different letters
  • @ Characters can only be placed in the first place of the identifier , belt @ The identifier of a prefix is called a verbatim identifier
  • Identifier cannot be used C# Keywords in , But add @ Can be a legal identifier But this is not recommended
  • Cannot be associated with C# The class library name of is the same
    • *

3、 What are keywords

We can see in this simple program using、 namespace、 class、 These are all C# Predefined retention identifier These pre reserved identifiers have a special meaning for the compiler We can't use it as an identifier for our program ( The identifier cannot duplicate the keyword Except with @)

For example, we define a int Variable A variable called using So the program will report an error

Engelbart

because using Is a pre-defined reserved identifier that helps us refer to other namespace ( What's in a reference namespace and so on ) So we can't use it as an identifier for our own variables

int @using = 1;

close @ Can

C# A lot of keywords are provided It's good to know about it at the beginning You don't need to remember everything Because a lot of keywords will be used frequently As time goes by, I can remember it all Here attached C# Official C# Keyword document If you don't know, it's over

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


4、 Why reference a namespace

Now let's take a look at the concept of reference namespace

The reason why we can call Console.WriteLine(); This method ( You can also call it a function ) Because we quoted System This namespace , because Console.WriteLine() This method is based on using System Under the namespace , If we don't quote System This namespace Let's see what happens

We put using System This sentence is taken out

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

You can see that the compiler reported an error !

Engelbart

Indicates that... Does not exist in the current context Console Let's put System Quote back

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

Engelbart The compilation is normal again

Let's have a deeper understanding of that sentence The reason why we can call Console.WriteLine(); This method ( You can also call it a function ) Because we quoted System This namespace , because Console.WriteLine() This method is based on using System Under the namespace

Mouse click Console Press down F12 Or right-click to go to definition Let's see

Engelbart

Can be observed System Defines a Console class

And then we pull down perhaps Back to our program, click WriteLine And then again F12

Engelbart

Did you see WriteLine() This function

So now I get it Console.WriteLine() stay System Under the namespace Console Class If we don't quote System Namespace Naturally, it can't be called It's like in real life You need to go to your friend's house and borrow his PS4 play You don't know where his house is How can you borrow it from him

版权声明
本文为[NewIDream]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210124101259799X.html

随机推荐