NET 4TH UNIT
NET 4TH UNIT
1. System.IO Namespace
The System.IO namespace provides classes for performing input/output operations in C#.
These operations include reading and writing to files, data streams, and managing file
systems.
Key Classes:
o File and FileInfo: Work with files, such as creation, deletion, copying,
moving, and retrieving file details.
o Directory and DirectoryInfo: Manage directories, including creation,
deletion, and listing files or subdirectories.
o Path: Provides methods for manipulating path strings (e.g., GetFileName,
GetExtension, Combine).
o Stream: Abstract base class for handling data as byte streams.
Common Stream Types:
FileStream: For reading/writing to files.
MemoryStream: Stores data in memory.
BufferedStream: Adds a buffer to read/write operations to optimize performance.
NetworkStream: Used for network communication.
Examples:
Writing to a File:
using System.IO;
// Writing to a file
using (StreamWriter writer = new StreamWriter("example.txt")) {
writer.WriteLine("Hello, World!");
}
4. Object Serialization
Serialization is the process of converting an object into a format that can be stored or
transmitted. Deserialization reconstructs the object from this format.
Types of Serialization:
o Binary Serialization: Saves object state in binary format.
o XML Serialization: Saves object state in XML format.
o JSON Serialization: Saves object state in JSON format.
Common Formatters:
o BinaryFormatter (deprecated, use alternatives like JSON or XML).
o XmlSerializer.
o DataContractSerializer.
Example:
Binary Serialization:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
public class Person {
public string Name { get; set; }
public int Age { get; set; }
}
// Serialize
Person person = new Person { Name = "John", Age = 30 };
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream = new FileStream("person.bin", FileMode.Create)) {
formatter.Serialize(stream, person);
}
// Deserialize
using (FileStream stream = new FileStream("person.bin", FileMode.Open)) {
Person deserializedPerson = (Person)formatter.Deserialize(stream);
Console.WriteLine($"Name: {deserializedPerson.Name}, Age:
{deserializedPerson.Age}");
}
5. Remoting
Remoting enables communication between objects in different application domains or
processes.
Key Components:
o Remotable Object: Inherits from MarshalByRefObject.
o Channel: Handles communication (e.g., TcpChannel, HttpChannel).
o Remote Interface: Defines the contract for remote communication.
6. ADO.NET
ADO.NET is a framework for interacting with databases.
Connected Model: Works with SqlConnection and SqlCommand for direct database
interaction.
Disconnected Model: Uses DataSet and DataTable to fetch data and work with it
locally.
Key Components:
Connection: Establishes a connection to the database (e.g., SqlConnection).
Command: Executes SQL queries (e.g., SqlCommand).
DataReader: Provides a forward-only, read-only data stream.
DataAdapter: Facilitates interaction between the database and DataSet.
Example:
Connecting to a Database:
using System.Data.SqlClient;