C# getting started tutorial-1

Using ide or online tools

1: Try changing the following code to output "Hello, universe" instead of "Hello, world".

using System;

public class program
{
	public static void Main()
	{
		Console.WriteLine("Hello, world");
	}
}

2: Contents of this chapter

Basic structure of C# program

How to use console WriteLine

How to use

How to use annotations

3:Main function

By default, C# will execute a function (method) called Main as the starting point of the application, which is the entry point of the application. For more information about entry points, see

Main() and command-line arguments | Microsoft Docs

We can define a class named program, as follows:

using System;

public class program
{

}

C # uses braces {and} to represent the code block. Now add the Main function.

using System;

public class program
{
    public static void Main()
    {

    }
}

public static void will be introduced later

4:Console.WriteLine

How do I see "Hello, world" output to the screen in front of me?

using System;

public class program
{
    public static void Main()
    {
        // This is the code
        Console.WriteLine("Hello, world");
    }
}

Note: if the text to be output is in double quotation marks "", a semicolon must be added after calling the WriteLine method;

5:using

In the previous example, we used the following code to output "Hello, world" to Console.

Console.WriteLine("Hello, world");

However, where does Console come from, why and how can we use it? In order to use the Console class in our program, we need to add a using at the top.

using System;

Yes NET Framework organizes their classes into containers called namespaces. In my example, the Console class belongs to the System namespace NET Framework provides many namespaces. Here are some examples.

System.Collections - Contains interfaces and classes that define various collections of objects, such as lists, queues, bit groups, hash tables, and dictionaries.
System.IO - Contains the types of files and data streams that are allowed to read and write, as well as the types that provide basic file and directory support.
System.Xml - Provide standards based XML Processing support.

If you do not use using, you can also use a class belonging to a namespace with a fully qualified type name.

public class program
{
    public static void Main()
    {
        System.Console.WriteLine("Hello, world");
    }
}

Exercise: fix the following code to make the program output "Hello, world" with and without using, respectively

public class program
{
  public static void Main()
  {
    Console.WriteLine("Hello, world");
  }
}

Namespaces more information:

System.Net namespace () | Microsoft Docs

Understanding Namespaces | Microsoft Docs

using more information:

using instruction - C# reference | Microsoft Docs

6: Notes

See all the blue and gray text in the code below? All comments (default green for IDE)

using System;

public class program
{
    public static void Main()
    {
        // I'm ouo

        /*
          I am multiline comment OuO
        */

        Console.WriteLine("Hello, world");
    }
}

Notes are very important. I really didn't lie to you! Unless you're a humanoid computer: D

Let you remember what the use of your code is, and so on. I believe you will feel it after you join the work or join a team.

In C # there are several ways to add comments to your code.

Single-Line Comments

Comment out the line after / /

//Console.WriteLine("Hello, world");
Console.WriteLine("Hello, world"); // See you again. I'm the comment after the code: B.

multiline comment

/*All contents between and * / will become notes (note OS: it is a gift from God to become notes.)

/*
    I'm a note
    Me too.
    You too
*/

Exercise: use a single line comment to add console Writeline ("Hello, world"); notes.

using System;

public class program
{
    public static void Main()
    {
        Console.WriteLine("Hello, world");
    }
}

Combine the two lines of console with a multiline comment Writeline comment.

using System;

public class program
{
    public static void Main()
    {
        Console.WriteLine("Hello");
		Console.WriteLine("world");
    }
}

have NET related questions and doubts, Microsoft documentation please.

At the end of this tutorial, the next chapter will learn more about Console, which is your first step towards changing the world.

Keywords: C# microsoft

Added by curt3006 on Sun, 09 Jan 2022 23:30:59 +0200