INTRODUCTION TO C#
📘 Introduction to C#
✅ What is C#?
C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft.
👉 It is mainly used to build:
- Desktop applications
- Web applications
- APIs
- Enterprise software
✅ Why use C#?
- ✔ Simple and easy to learn
- ✔ Strongly typed (reduces errors)
- ✔ Supports Object-Oriented Programming (OOP)
- ✔ Works well with .NET ecosystem
- ✔ Widely used in industry
❌ Why NOT use C#?
- ❌ Not ideal for low-level system programming (like OS/kernel)
- ❌ Requires .NET runtime
- ❌ Less popular than JavaScript for frontend
✅ How C# Works (Basic Flow)
-
Write code in
.csfile - Compile using .NET compiler
- Run → Output shown in console/app
📘 Core Concepts Explained
1️⃣ namespace
✅ What
A namespace is a container for classes.
✅ Why
- Avoids naming conflicts
- Organizes code
✅ Example
namespace MyApp
{
}
2️⃣ internal
✅ What
An access modifier.
✅ Why
- Restricts access within the same project only
❌ Why not public?
-
publicexposes it everywhere (less secure)
3️⃣ class
✅ What
A blueprint for creating objects.
✅ Why
- Groups data and methods together
4️⃣ object
✅ What
Instance of a class.
✅ Why
- Used to access class properties and methods
5️⃣ static
✅ What
Belongs to class, not object.
✅ Why
- No need to create object to access it
6️⃣ void
✅ What
Method returns nothing.
✅ Why
- Used when no return value is needed
7️⃣ string[]
✅ What
Array of strings.
✅ Why
- Used to store multiple values
-
In
Main, it stores command-line arguments
8️⃣ Console.WriteLine()
✅ What
Prints output to console.
✅ Why
- Used for displaying messages
9️⃣ Console.ReadKey()
✅ What
Waits for a key press.
✅ Why
- Prevents console from closing immediately
💡 Real-Time Example
👉 Scenario: Display employee details in console
✅ Full Program with Comments
// Namespace: groups related classes
namespace CompanyApp
{
// internal: accessible only within this project
internal class Employee
{
// Member variables
string _name;
int _age;
// Method to set values
public void SetDetails(string name, int age)
{
_name = name;
_age = age;
}
// Method to display values
public void Display()
{
Console.WriteLine("Employee Name: " + _name);
Console.WriteLine("Employee Age: " + _age);
}
}
internal class Program
{
// Entry point of program
static void Main(string[] args) // string[] stores command-line inputs
{
// Creating object of Employee class
Employee emp = new Employee();
// Setting values
emp.SetDetails("Rahul", 25);
// Displaying output
emp.Display();
// Static method call (no object needed)
Console.WriteLine("Press any key to exit...");
// Wait for user input
Console.ReadKey();
}
}
}
🔄 Flow of Above Program
-
Program starts →
Main() -
Object
empis created - Data is assigned using method
- Data is displayed
- Message printed
- Program waits for key press
🎯 Key Takeaways
- namespace → organizes code
- internal → restricts access
- class → blueprint
- object → real instance
- static → no object required
- void → no return
- string[] args → input arguments
- Console.WriteLine() → output
- Console.ReadKey() → pause
✅ One-Line Summary
👉 C# is an object-oriented language where programs are built using classes, objects, and methods, and executed starting from the Main() method.
Comments
Post a Comment