C# / C# Basics

Introduction to C#

This tutorial is designed to introduce beginners to the basics of C#. It will provide a solid foundation in C# by covering key concepts and syntax.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Introduces the fundamental concepts of C#, including syntax, data types, and control structures.

Introduction to C

1. Introduction

Welcome to this beginner-friendly tutorial on C#. Our goal is to give you an understanding of the basics of C# programming. We will cover various key concepts and syntax, providing you a solid foundation to build upon.

By the end of this tutorial, you will learn:
- Basic Syntax of C#
- Data Types and Variables
- Control Structures
- Object-Oriented Programming (OOP) concepts

Prerequisites:
While no prior knowledge of C# is necessary, basic understanding of programming concepts like variables, loops, and functions would be beneficial.

2. Step-by-Step Guide

C# Syntax

C# is a statically-typed language, which means we need to declare the type of a variable when we declare it.

int number = 10; // declaring an int variable

Data Types and Variables

C# includes several different types of data including integers (int), floating point numbers (float, double), characters (char), and strings (string).

int i = 10; // Integer
double d = 20.5; // Double
char c = 'a'; // Character
string s = "hello"; // String

Control Structures

Control structures like if, else, for, and while are fundamental to any programming language, including C#.

// If else structure
if (i > 0)
{
    Console.WriteLine("Positive number");
}
else
{
    Console.WriteLine("Non-positive number");
}

// For loop
for (int j = 0; j < 5; j++)
{
    Console.WriteLine(j);
}

// While loop
while (i > 0)
{
    Console.WriteLine(i);
    i--;
}

Object-Oriented Programming (OOP)

C# is an object-oriented language, which means it allows us to encapsulate data and methods into objects.

class HelloClass
{
    private string message = "Hello, World!";

    public void DisplayMessage()
    {
        Console.WriteLine(message);
    }
}

HelloClass hello = new HelloClass();
hello.DisplayMessage(); // Outputs: Hello, World!

3. Code Examples

Example 1: Hello, World!

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}
// This will output: Hello, World!

Example 2: Data Types and Variables

using System;

class Program
{
    static void Main()
    {
        int number = 10; // Integer
        Console.WriteLine(number); // Outputs: 10
    }
}

Example 3: Control Structures

using System;

class Program
{
    static void Main()
    {
        for (int i = 0; i < 5; i++) 
        {
            Console.WriteLine(i); // Outputs: 0, 1, 2, 3, 4
        }
    }
}

4. Summary

In this tutorial, we have covered the basics of C# including syntax, data types, control structures, and OOP principles. These concepts form the foundation of C# programming.

To continue your learning journey, try creating simple applications, explore more about C# classes, interfaces, and delve into more advanced topics like LINQ and async programming.

5. Practice Exercises

Exercise 1: Write a program that asks the user for their name and greets them with their name.

Solution:

using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter your name: ");
        string name = Console.ReadLine();
        Console.WriteLine("Hello, " + name);
    }
}

Exercise 2: Write a program that prints the sum of two numbers.

Solution:

using System;

class Program
{
    static void Main()
    {
        int num1 = 10;
        int num2 = 20;
        int sum = num1 + num2;
        Console.WriteLine("The sum is " + sum); // Outputs: The sum is 30
    }
}

Exercise 3: Write a program that prints the multiplication table of a given number.

Solution:

using System;

class Program
{
    static void Main()
    {
        int num = 5;
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine(num + " * " + i + " = " + num * i);
        }
    }
}

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Unit Converter

Convert between different measurement units.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help