Introduction to C#
Welcome to IqraTechnology! Today, we will introduce you to C# programming language. C# is a powerful and versatile language developed by Microsoft. It is primarily used for building Windows applications, but it is also widely used in web, mobile, and game development.
C# is an object-oriented language, which means it is designed to help you create modular, reusable, and efficient code. Its robust features and capabilities make it an excellent choice for developing a wide range of applications.
Key Features of C#
Simplicity and Ease of Use: C# is designed to be simple and easy to learn, especially for those who are new to programming. Its syntax is straightforward and similar to other popular languages like C++ and Java.
Object-Oriented: C# supports object-oriented programming (OOP) principles such as encapsulation, inheritance, and polymorphism. This allows developers to create modular and maintainable code.
Type Safety: C# is a statically-typed language, meaning that type checking is done at compile-time rather than at run-time. This helps catch errors early in the development process and ensures that your code runs smoothly.
Rich Library and Framework Support: C# comes with a comprehensive standard library and is supported by the .NET framework. This provides a vast array of pre-built functionalities, making it easier to develop complex applications.
Cross-Platform Development: With the advent of .NET Core, C# can be used to build cross-platform applications that run on Windows, macOS, and Linux.
Getting Started with C#
To start coding in C#, you need an Integrated Development Environment (IDE) like Visual Studio. This powerful tool provides everything you need to write, debug, and run your C# programs.
Here is a simple example of a C# program that prints “Hello, World!” to the console:
using System;
namespace simplecode
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello, World!”);
}
}
}
This basic example demonstrates how easy it is to get started with C#. The Console.WriteLine method is used to output text to the console.
Reading Input from the Console
using System;
namespace ConsoleDemo
{
class Program
{
static void Main(string[] args)
{
// Declare some variables
int a, b, sum;
string input;
// Prompt the user to enter two numbers
Console.WriteLine(“Enter two numbers:”);
// Read the first number as a string using the ReadLine method
input = Console.ReadLine();
// Convert the string to an integer using the Convert.ToInt32 method
a = Convert.ToInt32(input);
// Read the second number as a string using the ReadLine method
input = Console.ReadLine();
// Convert the string to an integer using the Convert.ToInt32 method
b = Convert.ToInt32(input);
// Calculate the sum of the two numbers
sum = a + b;
// Write the sum to the console using the WriteLine method
Console.WriteLine(“The sum is {0}”, sum);
Console.ReadLine();
}
}
}
The output of the program is:
Enter two numbers:
5
6
The sum is 11
Changing the Appearance and Behavior of the Console
using System;
namespace ConsoleDemo
{
class Program
{
static void Main(string[] args)
{
// Set the title of the console window using the Title property
Console.Title = “Console Demo”;
// Set the background color of the console to blue using the BackgroundColor property
Console.BackgroundColor = ConsoleColor.Blue;
// Set the foreground color of the console to white using the ForegroundColor property
Console.ForegroundColor = ConsoleColor.White;
// Clear the console screen using the Clear method
Console.Clear();
// Write a message to the console using the WriteLine method
Console.WriteLine(“Hello, World!”);
// Set the cursor size to 50% using the CursorSize property
Console.CursorSize = 50;
// Move the cursor to the next line using the WriteLine method
Console.WriteLine();
// Play a beep sound using the Beep method
Console.Beep();
// Reset the console colors to their defaults using the ResetColor method
Console.ResetColor();
// Write a message to the console using the WriteLine method
Console.WriteLine(“Goodbye, World!”);
}
}
}
The output of the program is:
Conclusion
C# is a versatile and powerful language that is ideal for a wide range of programming tasks. Whether you are developing desktop applications, web applications, or games, C# provides the tools and features you need to create high-quality software.