C# Programming Guide

Welcome to the C# Programming Guide. This guide is designed for beginners and those looking to enhance their C# skills.

C#

This guide explores the basics of C#, covering core concepts, syntax, and best practices.

Introduction

C# is a powerful and versatile programming language developed by Microsoft.

It's primarily used for building Windows applications, web applications with ASP.NET Core, and game development with Unity.

Key features include strong type safety, object-oriented programming, and easy integration with .NET.

Key Concepts

Let's start with a simple example:

                
                    @param int a
                    @return int
                
                
                    @param string b
                    @return string
                
            

Basic Syntax

In C#, you write code using curly braces `{}` to define code blocks. The syntax for the main code is:

                
                    @for i = 0 to 10 {
                        print(i);
                    }
                    @endfor
                
            

Variables & Data Types

Variables are named storage locations that hold data. C# has several data types:

  • int: Integer numbers (whole numbers)
  • float: Floating-point numbers (numbers with decimal points)
  • double: Double-precision floating-point numbers
  • bool: Boolean values (true or false)
  • string: Textual data

Methods & Properties

Methods are functions that perform specific tasks. Properties are attributes that define the data of an object.

Example:

                
                    @public class Person {
                        public string Name { get; set; }
                        public int Age { get; set; }
                    }
                    @example
                    @Person{Name: "Alice", Age: 30}
                
            

Control Flow

C# supports control flow statements: if/else, loops, and switch.

If/Else: Allows you to execute different blocks of code based on a condition.

Loops: Repeat a block of code multiple times.

Switch: Select a value from a set of options.

Code Block Example

                
                    @for (int i = 0; i < 5; i++) {
                        print(i);
                    }
                    @endfor