-
Notifications
You must be signed in to change notification settings - Fork 5
Using Keyword in C#
Sann Lynn Htun edited this page Nov 21, 2024
·
1 revision
The using
keyword in C# is a powerful feature that simplifies resource management, namespace inclusion, and type aliasing. It has several key uses that are essential for efficient and clean code development in C#.
-
Namespace Inclusion
- The most common use of the
using
keyword is to include namespaces in your code. This allows you to access classes, methods, and other members defined in those namespaces without needing to fully qualify their names.
- The most common use of the
-
Disposable Resource Management
- The
using
statement is also used to ensure that objects that implement theIDisposable
interface are properly disposed of, freeing up resources and improving application performance and reliability.
- The
-
Type Aliasing
- The
using
directive can create an alias for a long or complex type name, making your code easier to read and maintain.
- The
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
Console.WriteLine("Numbers: " + string.Join(", ", numbers));
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine("Hello, World!");
} // StreamWriter is disposed of here, closing the file.
}
}
using System;
using Project = System.Collections.Generic.Dictionary<string, string>;
class Program
{
static void Main()
{
Project projectDetails = new Project
{
{ "Name", "C# Project" },
{ "Type", "Console Application" }
};
foreach (var detail in projectDetails)
{
Console.WriteLine($"{detail.Key}: {detail.Value}");
}
}
}
-
Static Using Statements
- Allows you to import static members of a class so you can use them without qualifying the class name.
using static System.Math;
class Program
{
static void Main()
{
double result = Sqrt(16); // No need to qualify with Math.Sqrt
Console.WriteLine(result);
}
}
-
Global Using Statements (C# 10)
- Introduced in C# 10, these allow you to define
using
directives at the project level so they are available in all files within the project.
- Introduced in C# 10, these allow you to define
// GlobalUsings.cs
global using System;
global using System.Collections.Generic;
- Namespace Inclusion: Simplifies the code by allowing access to types without fully qualifying their names.
-
Disposable Resource Management: Ensures resources are properly released with the
using
statement. - Type Aliasing: Improves readability by creating shorter names for complex types.
- Static Using: Provides a way to use static members without class qualification.
-
Global Using: Makes
using
directives available across the entire project.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.