C#/C# Tutorial

C# Basic Syntax | C# Tutorial for Beginners

DragonTory 2023. 2. 14. 17:45
반응형

 C# Overview 

C# (pronounced "C sharp") is a modern, object-oriented programming language that was developed by Microsoft.

It was first introduced in 2000 as part of Microsoft's .NET Framework, and since then it has become a popular choice for developing a wide variety of applications, including desktop software, web applications, and mobile apps. C# is designed to be simple, efficient, and type-safe, and it has features such as garbage collection, automatic memory management, and strong typing that make it easier to write and maintain complex programs.

C# is used in combination with the .NET Framework to create applications for Windows and other platforms, and it is often used in conjunction with other languages such as HTML, CSS, and JavaScript to create complete web applications.

 C# Environment 

To develop C# applications, you need a development environment that includes a C# compiler, a code editor, and other tools. 

Visual Studio is a powerful Integrated Development Environment (IDE) for C# and other .NET programming languages. It is developed by Microsoft and provides a comprehensive set of tools and features for C# development, including:

  • Code editor: Visual Studio's code editor provides syntax highlighting, code completion, and other features to help you write and edit C# code more efficiently.
  • Debugger: The debugger in Visual Studio allows you to step through your code and inspect variables and objects to diagnose issues.
  • Project templates: Visual Studio provides a variety of project templates for C# development, including console applications, Windows Forms applications, web applications, and more.
  • NuGet package manager: NuGet is a package manager for .NET libraries and dependencies, and Visual Studio includes a built-in NuGet package manager to help you manage your project dependencies.
  • Git integration: Visual Studio integrates with Git and other version control systems to help you manage your code changes and collaborate with other developers.
  • Performance profiler: Visual Studio includes a performance profiler to help you identify performance bottlenecks in your code.

    Visual Studio is available in different editions, including Community (free), Professional, and Enterprise, and it can be used to develop C# applications for Windows, Web, and Mobile platforms.

 

 C# Basic Syntax 

Here is an overview of basic C# syntax:

  • Class declaration: In C#, everything is declared within a class. To create a class, use the class keyword, followed by the class name.
  • Method declaration: Methods are functions that are defined within a class. To create a method, use the public keyword (or another access modifier), followed by the return type, the method name, and any parameters.
  • Main method: In a C# console application, the Main method is the entry point of the program. It has the signature static void Main(string[] args), where string[] args is an array of command-line arguments.
  • Statements: C# code is made up of statements, which are executed one at a time. Common statements include assignment statements, control flow statements (like if and while), and method calls.
  • Variables: Variables are used to store data. To create a variable, specify the variable type, followed by the variable name. Variables can be assigned a value using the assignment operator =.
  • Comments: Comments are used to add notes to your code. In C#, you can use // to create a single-line comment, or /* */ to create a multi-line comment.

 

Here is an example of C# code that declares a class, a method, and a variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
using System;
 
class Program
{
    static void Main(string[] args)
    {
        int x = 10;
        int y = 5;
        int z = x + y;
 
        Console.WriteLine("The sum of {0} and {1} is {2}.", x, y, z);
    }
}
 
cs

 

In this example, we are:

  • Using the using keyword to include the System namespace, which provides access to the Console class.
  • Defining a class called Program.
  • Defining a static method called Main that takes an array of strings called args.
  • Declaring three variables: x is assigned the value 10, y is assigned the value 5, and z is assigned the value of x + y.
  • Calling the Console.WriteLine method to print a message to the console that includes the values of x, y, and z.

When you run this program, it will output the message "The sum of 10 and 5 is 15." to the console.

 

 C# Comments 

In C#, there are two ways to create comments: single-line comments and multi-line comments.

Single-line comments start with two forward slashes (//) and continue until the end of the line. 

Here's an example:

1
// This is a single-line comment
cs

 

Multi-line comments start with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/).

Here's an example:

1
2
/* This is a
   multi-line comment */
cs

 

Comments are used to add notes to your code that help you and others understand what the code is doing. It's a good practice to include comments in your code to make it easier to read and maintain.

반응형