0% found this document useful (0 votes)
17 views4 pages

Unit 1

C# is a modern, object-oriented programming language developed by Microsoft for the .NET framework, used for various applications including desktop and web. Key features include type safety, garbage collection, and platform independence, while the CLR serves as the execution engine managing memory and security. The document also covers creating console applications, Windows Forms, and fundamental C# concepts such as data types, enumerations, structures, and namespaces.

Uploaded by

Hrithik Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Unit 1

C# is a modern, object-oriented programming language developed by Microsoft for the .NET framework, used for various applications including desktop and web. Key features include type safety, garbage collection, and platform independence, while the CLR serves as the execution engine managing memory and security. The document also covers creating console applications, Windows Forms, and fundamental C# concepts such as data types, enumerations, structures, and namespaces.

Uploaded by

Hrithik Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to C#

C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by


Microsoft as part of the .NET framework. It is widely used for building desktop applications, web
applications, games, and mobile apps.
Key Features of C#:
Object-Oriented: Supports encapsulation, inheritance, and polymorphism.
Type-Safe: Ensures code reliability by enforcing strict type-checking.
Garbage Collection: Automatic memory management prevents memory leaks.
Platform Independence: Runs on multiple platforms using the .NET runtime.
Rich Libraries: Provides access to a wide range of libraries for various functionalities.

1. CLR (Common Language Runtime)


The Common Language Runtime (CLR) is the execution engine of the .NET framework. It
provides an environment for running and managing .NET applications.
Key Features of CLR:
Just-In-Time (JIT) Compilation: Converts Intermediate Language (IL) code into native
machine code.
Memory Management: Handles allocation and deallocation of memory automatically.
Garbage Collection: Cleans up unused objects to free memory.
Exception Handling: Provides a consistent mechanism for handling runtime errors.
Security: Implements code access security and verification.
CLR Workflow:
1. Source code is compiled into Intermediate Language (IL).
2. IL code is converted to native machine code by the JIT compiler.
3. Native code is executed by the CLR.

2. Visual Studio Console Application


A Console Application is a simple C# program that runs in a command-line interface (CLI)
window. It is used for learning, testing, or automating simple tasks.
Steps to Create a Console Application:
1. Open Visual Studio.
2. Click Create a new project and select Console App (.NET Core) or Console App (.NET
Framework).
3. Write your code in the Main() method, which is the entry point of the application.
Example:
csharp Copy code
using System; class Program { static void Main(string[] args) {
Console.WriteLine("Hello, World!"); Console.WriteLine("Enter your name:");
string name = Console.ReadLine(); Console.WriteLine($"Hello, {name}!"); } }

3. Simple Windows Forms


Windows Forms is a graphical user interface (GUI) framework for building desktop applications.
Steps to Create a Windows Forms Application:
1. Open Visual Studio.
2. Click Create a new project and select Windows Forms App (.NET Framework).
3. Drag and drop controls (e.g., buttons, labels, textboxes) onto the form using the toolbox.
4. Double-click controls to add event handlers (e.g., button click events).
Example:
Design:
Add a Button and a Label to the form.
Code (Form1.cs):
csharp Copy code
private void button1_Click(object sender, EventArgs e) { label1.Text =
"Hello, Windows Forms!"; }

4. C# Language Fundamentals
a) Data Types:
C# supports primitive types (e.g., int , float , bool ) and reference types (e.g., string ,
arrays, classes).
b) Variables and Constants:
Variables store data, while constants hold values that cannot be changed.
csharp Copy code
int age = 25; const double Pi = 3.14;

c) Operators:
C# includes arithmetic ( + , - ), relational ( == , > ), and logical operators ( && , || ).
d) Control Statements:
If-Else:
csharp Copy code
if (age > 18) Console.WriteLine("Adult"); else Console.WriteLine("Minor");

Loops:
csharp Copy code
for (int i = 0; i < 5; i++) Console.WriteLine(i);

5. Enumerations
An Enumeration (enum) is a distinct type that defines a set of named constants.
Syntax:
csharp Copy code
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
class Program { static void Main() { Days today = Days.Wednesday;
Console.WriteLine($"Today is {today}"); } }

6. Structures
A Structure (struct) is a value type that groups related variables under one name.
Syntax:
csharp Copy code
struct Point { public int X; public int Y; public Point(int x, int y) { X = x;
Y = y; } } class Program { static void Main() { Point p = new Point(10, 20);
Console.WriteLine($"Point: ({p.X}, {p.Y})"); } }

7. Namespaces
A Namespace organizes classes and other types to avoid naming conflicts. It acts as a container
for related functionalities.
Syntax:
csharp Copy code
namespace MyNamespace { class MyClass { public void SayHello() {
Console.WriteLine("Hello from MyNamespace!"); } } } class Program { static void
Main() { MyNamespace.MyClass obj = new MyNamespace.MyClass(); obj.SayHello(); }
}

Common Namespaces in C#:


System : Core classes like Console , Math , DateTime .
System.Collections.Generic : Generic collections like List<T> , Dictionary<TKey,
TValue> .
System.IO : File I/O operations.

Summary
1. C#: A modern, object-oriented programming language designed for .NET applications.
2. CLR: The runtime environment for executing .NET applications with features like memory
management and JIT compilation.
3. Console Apps: Simple CLI-based applications for learning and testing.
4. Windows Forms: GUI framework for building interactive desktop applications.
5. C# Fundamentals: Core concepts like data types, control statements, and operators.
6. Enumerations: Define a set of named constants.
7. Structures: Group related variables into a value type.
8. Namespaces: Organize classes to avoid naming conflicts and improve code readability.

You might also like