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.
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
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 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--;
}
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!
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
// This will output: Hello, World!
using System;
class Program
{
static void Main()
{
int number = 10; // Integer
Console.WriteLine(number); // Outputs: 10
}
}
using System;
class Program
{
static void Main()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i); // Outputs: 0, 1, 2, 3, 4
}
}
}
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.
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);
}
}
}