Comprehensive C# Tutorial
Introduction
C# is a modern, object-oriented programming language developed by Microsoft. It is used for a variety of applications, from web development to game development and enterprise-level applications. In this tutorial, we will cover the basics of C# programming, including syntax, data types, control structures, and object-oriented programming concepts.
Getting Started
To start programming in C#, you need a development environment. The most popular choice is Visual Studio, which provides a powerful IDE for C# development.
Download and install Visual Studio from here.
First C# Program
Let's start with a simple "Hello, World!" program to understand the structure of a C# application.
using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
Here, using System;
imports the System namespace, which contains fundamental classes and base classes that define commonly-used data types, events, and event handlers, interfaces, attributes, and processing exceptions.
The namespace
keyword is used to declare a scope that contains a set of related objects. HelloWorld
is the name of the namespace.
The class
keyword is used to define a class. Program
is the name of the class.
The Main
method is the entry point of a C# application. Console.WriteLine
is used to print the specified message to the console.
Variables and Data Types
Variables are used to store data in a program. C# is a strongly-typed language, which means that every variable must have a type.
int age = 25; double salary = 50000.75; char grade = 'A'; string name = "John Doe"; bool isEmployed = true;
Here, we have declared variables of different data types:
int
: Integer data typedouble
: Floating-point data typechar
: Character data typestring
: String data typebool
: Boolean data type
Control Structures
Control structures are used to control the flow of execution in a program. C# supports various control structures, including conditional statements, loops, and switch statements.
Conditional Statements
int age = 18; if (age >= 18) { Console.WriteLine("You are an adult."); } else { Console.WriteLine("You are not an adult."); }
Loops
for (int i = 0; i < 5; i++) { Console.WriteLine("Value of i: " + i); }
Object-Oriented Programming
C# is an object-oriented programming language, which means it supports the concepts of classes and objects, inheritance, polymorphism, encapsulation, and abstraction.
Classes and Objects
class Person { public string Name { get; set; } public int Age { get; set; } public void Introduce() { Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old."); } } Person person = new Person(); person.Name = "John Doe"; person.Age = 30; person.Introduce();
Here, we have defined a Person
class with two properties (Name
and Age
) and a method (Introduce
). We then create an instance of the Person
class and call the Introduce
method.
Conclusion
In this tutorial, we have covered the basics of C# programming, including syntax, data types, control structures, and object-oriented programming concepts. With this foundation, you can start building more complex applications and explore advanced features of the C# language.