Overview of C#
C# (pronounced "C-Sharp") is a modern, versatile programming language developed by Microsoft. It is part of the .NET framework and is widely used for building Windows applications, web applications, and games. In this tutorial, we provide an overview of C# programming concepts with examples.
Step 1: Key Features of C#
C# offers many powerful features, including:
- Object-Oriented: Supports classes, objects, inheritance, and polymorphism.
- Type-Safe: Ensures that variables and objects are accessed in a safe way.
- Scalable and Versatile: Can be used to create small scripts to large enterprise applications.
- Part of .NET Framework: Offers a rich set of libraries and tools for development.
Step 2: Writing a Simple Program
Let’s start with a simple program to display an overview message in the console.
using System; class Overview { static void Main() { Console.WriteLine("Welcome to C# Programming!"); Console.WriteLine("This language supports object-oriented programming, type safety, and scalability."); } }
Save the code and run it to see the output.
Result:
The output in the console will be:
Welcome to C# Programming! This language supports object-oriented programming, type safety, and scalability.
Step 3: Exploring Data Types
C# supports various data types for storing values. Let’s look at an example:
using System; class DataTypes { static void Main() { int number = 10; double pi = 3.14; string message = "C# is powerful!"; bool isCSharpFun = true; Console.WriteLine("Integer: " + number); Console.WriteLine("Double: " + pi); Console.WriteLine("String: " + message); Console.WriteLine("Boolean: " + isCSharpFun); } }
Result:
The output in the console will be:
Integer: 10 Double: 3.14 String: C# is powerful! Boolean: True
Step 4: Using Control Statements
C# supports various control statements such as if-else and loops. Here is an example:
using System; class ControlStatements { static void Main() { int number = 5; if (number > 0) { Console.WriteLine("The number is positive."); } else { Console.WriteLine("The number is not positive."); } Console.WriteLine("Loop example:"); for (int i = 1; i <= 5; i++) { Console.WriteLine("Count: " + i); } } }
Result:
The output in the console will be:
The number is positive. Loop example: Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
Step 5: Understanding Methods
Methods are blocks of code that perform specific tasks. Here’s an example:
using System; class MethodsExample { static void Greet() { Console.WriteLine("Hello from a method!"); } static void Main() { Greet(); } }
Result:
The output in the console will be:
Hello from a method!
Step 6: Conclusion
In this tutorial, you learned about the key features of C#, basic syntax, data types, control statements, and methods. C# is a robust and flexible language suitable for various applications. Explore further to unlock its full potential!