You are on page 1of 9

Intefaces:

using System;

class Program
{
static void Main(string[] args)
{
Car a = new Car(1236, 2002, "bmw");
Car b = new Car(1236, 2002, "bmw");
Console.WriteLine(a.equal(b));
}

}
interface IEqual<x>
{
bool equal(x var);
}
class Car:IEqual<Car>
{
public Car(int ID, int year, string maker) => (this.ID,
this.year, this.maker)=(ID,year,maker);
int ID, year;
string maker;
public bool equal(Car x)
{
return (this.ID, this.year, this.maker) ==
(x.ID,x.year,x.maker);
}

}
class bike:IEqual<bike>
{
public bike(int ID, int year, string maker) => (this.ID,
this.year, this.maker) = (ID, year, maker);
int ID, year;
string maker;
public bool equal(bike x)
{
return (this.ID, this.year, this.maker) == (x.ID, x.year,
x.maker);
}
}
Sorting:

using System;

class Program
{
static void Main(string[] args)
{
point[] myArr = { new point('a', 2, 2, 7), new point('b',
7, 0, 1) };
Array.Sort(myArr, (x, y) => (Math.Sqrt(x.x * x.x + x.y *
x.y + x.z * x.z).CompareTo((Math.Sqrt(y.x * y.x + y.y * y.y + y.z
* y.z)))));
Console.WriteLine(myArr[0]);
Console.WriteLine(myArr[1]);

}
class point
{
public point(char name, int x,int y ,int z)
{
this.name = name;
this.x = x;
this.y = y;
this.z = z;
}
public int x, y, z;
char name;
public override string ToString()
{
return $"{name} ({x},{y},{z}) ";
}

}
Lamda Expressions:
using System;
using System.ComponentModel.DataAnnotations;

class Program
{
static int Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4,
5 };
List<int> subset = numbers.FindAll(t => t > 0);
display(subset);
subset = numbers.FindAll(t => t>3);
display(subset);
subset = numbers.FindAll(t => t%2==0);
display(subset);

return 4;
}
static void display(List<int> a)
{
foreach(int x in a) Console.Write(x + " ");
Console.WriteLine();
}
}

Files and Streams:


using System.IO;
using System;

string filePath = "C:\\Users\\PC\\Desktop\\hassan.txt";

if (File.Exists(filePath))
{
string[] lines = File.ReadAllLines(filePath);

foreach (string line in lines)


{
Console.WriteLine(line);
}
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("we wrote this!");
writer.WriteLine("This is a sample text.");

}
}
else
{
Console.WriteLine("File does not exist.");
}

Stream Basics: A stream represents a sequence of bytes, and it can be used to read or write
data. It provides a common set of methods and properties to work with data, regardless of the
specific data source or destination. Streams can be categorized into two main types: input
streams (readers) and output streams (writers).
Input Streams: Input streams are used to read data from a source. Some commonly used input
stream classes in C# include:
StreamReader: Reads characters from a stream in a specific encoding.
BinaryReader: Reads primitive data types from a stream in a specific format.
FileStream: Reads bytes from a file.
MemoryStream: Reads bytes from a block of memory.
Output Streams: Output streams are used to write data to a destination. Some commonly used
output stream classes in C# include:
StreamWriter: Writes characters to a stream in a specific encoding.
BinaryWriter: Writes primitive data types to a stream in a specific format.
FileStream: Writes bytes to a file.
MemoryStream: Writes bytes to a block of memory.
Stream Operations: Streams provide methods to perform common operations such as reading,
writing, seeking, and closing. Here are some common operations performed on streams:
Read: Reads a sequence of bytes or characters from the stream.
Write: Writes a sequence of bytes or characters to the stream.
Seek: Sets the position within the stream.
Flush: Flushes any buffered data to the underlying storage.
Close: Closes the stream and releases any resources associated with it.
Stream Usage: Streams are typically used within a using statement to ensure proper disposal of
resources. The using statement automatically calls the Dispose() method on the stream,
releasing any system resources it is using.

using System.IO;
using System;

FileStream sourceFileStream = new FileStream("C:\\


Users\\PC\\Desktop\\hassan.txt", FileMode.Open,
FileAccess.Read);
StreamReader read = new StreamReader(sourceFileStream);

FileStream destinationFileStream = new FileStream("C:\\


Users\\PC\\Desktop\\hassan2.txt", FileMode.Open,
FileAccess.Write);
StreamWriter writer = new
StreamWriter(destinationFileStream);

string? copypaste=null;
do
{
copypaste = read.ReadLine();
if (copypaste == null)
break;
writer.WriteLine(copypaste);

} while (true);
read.Close();
writer.Close();

using System;
using System.IO;

string filePath = "path/to/file.txt";

try
{
// Open the file in append mode using FileStream
using (FileStream fileStream = new
FileStream(filePath, FileMode.Append,
FileAccess.Write))
{
// Create a StreamWriter to write to the file
using (StreamWriter writer = new
StreamWriter(fileStream))
{
writer.WriteLine("New line to append");
writer.WriteLine("Another line to append");
}
}

Console.WriteLine("Data appended to the file


successfully.");
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " +
ex.Message);
}

Serialization:
void GetObjectData(SerializationInfo info, StreamingContext context);
protected ClassName(SerializationInfo info, StreamingContext context);

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
public class Person : ISerializable
{
public string Name { get; set; }
public int Age { get; set; }

public Person() { }
protected Person(SerializationInfo info,
StreamingContext context)
{

Name = info.GetString("Name");
Age = info.GetInt32("Age");
}

public void GetObjectData(SerializationInfo info,


StreamingContext context)
{
info.AddValue("Name", Name);
info.AddValue("Age", Age);
}
}
#pragma warning disable SYSLIB0011
public class SerializationExample
{
public static void Main()
{
Person person = new Person { Name = "John Doe",
Age = 30 };

BinaryFormatter formatter = new


BinaryFormatter();
using (FileStream fileStream = new
FileStream("person.dat", FileMode.Create))
{
formatter.Serialize(fileStream, person);
}

using (FileStream fileStream = new


FileStream("person.dat", FileMode.Open))
{
Person deserializedPerson =
(Person)formatter.Deserialize(fileStream);
Console.WriteLine("Name: " +
deserializedPerson.Name);
Console.WriteLine("Age: " +
deserializedPerson.Age);
}
}
}

You might also like