0% found this document useful (0 votes)
14 views5 pages

NET 4TH UNIT

Unit 4 of .net

Uploaded by

ixiixi439
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)
14 views5 pages

NET 4TH UNIT

Unit 4 of .net

Uploaded by

ixiixi439
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/ 5

UNIT-4

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;

File.WriteAllText("example.txt", "Hello, World!");


Reading from a File:
string content = File.ReadAllText("example.txt");
Console.WriteLine(content);

2. TextWriter and TextReader


 TextWriter: Abstract class for writing characters to a stream.
o Examples: StreamWriter, StringWriter.
 TextReader: Abstract class for reading characters from a stream.
o Examples: StreamReader, StringReader.
Example:
Using StreamWriter and StreamReader:
using System.IO;

// Writing to a file
using (StreamWriter writer = new StreamWriter("example.txt")) {
writer.WriteLine("Hello, World!");
}

// Reading from a file


using (StreamReader reader = new StreamReader("example.txt")) {
string line = reader.ReadLine();
Console.WriteLine(line);
}

3. BinaryWriter and BinaryReader


 BinaryWriter: Writes primitive data types in binary format.
 BinaryReader: Reads primitive data types in binary format.
Example:
Writing and Reading Binary Data:
using System.IO;

// Writing binary data


using (BinaryWriter writer = new BinaryWriter(File.Open("data.bin", FileMode.Create))) {
writer.Write(42); // Integer
writer.Write(3.14); // Double
writer.Write("Hello"); // String
}

// Reading binary data


using (BinaryReader reader = new BinaryReader(File.Open("data.bin", FileMode.Open))) {
Console.WriteLine(reader.ReadInt32()); // 42
Console.WriteLine(reader.ReadDouble()); // 3.14
Console.WriteLine(reader.ReadString()); // Hello
}

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;

string connectionString = "your_connection_string";


using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand("SELECT * FROM Students", connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
Console.WriteLine(reader["Name"]);
}
}

7. C# Windows Forms for Data Control


Windows Forms provides a graphical interface for applications, including tools for data
display and manipulation.
 DataGridView: Displays tabular data.
 DataSource: Binds data to controls like DataGridView.
 DataBinding: Links UI elements to data sources.
Example:
Binding Data to DataGridView:
using System.Data;
using System.Data.SqlClient;

SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Students",


connectionString);
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;

You might also like