Write to Text File in C#

First, create a new console app: Next, add: using System.IO; Now add code: using (StreamWriter writer = new StreamWriter(“filename.txt”)) { writer.Write(“1”); writer.WriteLine(“2”); writer.WriteLine(“3”); writer.Write(“4”); } Now go to the directory you ran the code from. You will see the filename.txt file created: This code can also be written as: StreamWriter writer = new StreamWriter(“filename.txt”); writer.Write(“1”); writer.WriteLine(“2”); writer.WriteLine(“3”); writer.Write(“4”); writer.Flush(); writer.Close();  

Using the Windows API

In this post, we will look at how to use the Windows API to get and set windows in the Windows operating system. First, create a new console app: We will firstly add the Interop Services, which allows us to enable interop with DLLs: using System.Runtime.InteropServices; In our case, we will be using the user32.dll. Next, we will use DllImport, which is part of the namespace we added, to reference … Continue reading Using the Windows API

Installing and Using ILSpy

ILSpy is an assembly browser and decompiler. Here we will go through installing and using ILSpy. First, download the binaries from the ILSpy website: This will download a ZIP file. Extract the file: Run the ISpy.exe: Click to open a file: Here I have selected an exe compiled from code I previously wrote. You can see we can now see the decompiled code within ILSpy:  

Serializing an Object in C#

Serialization is the process of converting an object into a byte stream. Once serialized, it can be transmitted to a file, a database or to memory or send it across a network. For example, you may want to convert data to XML. Serialization allows you to store the state of the object; you can then recreate it as needed. Let’s go through a simple example of serializing a Car object. … Continue reading Serializing an Object in C#

Abstract Classes in C#

Abstract classes are a type of class in C# that contains a base class implementation. The abstract class is not instantiated, and are inherited by subclasses to provide a detailed implementation. Consider the example below. We have an abtract class called Vehicle in our console app. We try to create an instance of the abtract class Vehicle: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Carl.AbstractClass { … Continue reading Abstract Classes in C#

Linq and Lambda in C#

LINQ, or Language Integrated Query, is a query language in the .NET Framework. LINQ is useful in that you could have several different data sources you are working with within Visual Studio, and LINQ provides a way to query data uniformly. Examples include SQL, Objects, ADO.NET, Entity Framework etc. Here we will go through examples of using LINQ. Firstly, to use LINQ, you add the namespace: using System.Linq; Let’s say … Continue reading Linq and Lambda in C#

Anonymous Functions in C#

Anonymous functions in C# are basically functions without a name that can be used in different situations. Let’s take a look at some examples. When looking at delegates, we saw how to pass delegate functions as parameters. With anonymous functions, we can define the delegate and then pass an anonymous function to it. In the example below, we are creating a delegate and then creating the anonymous function code, then … Continue reading Anonymous Functions in C#

C# Delegates

Delegates in C# hold a reference to a method. If you think about writing methods, you may generally send parameters such as objects/type variables to the method. At times, you may want to send an actual method as a parameter to another method.┬áDelegates are like a placeholder for methods. Let’s go through an example. Let’s say we have a list of customers that are based on different cities. Customer c1 … Continue reading C# Delegates

Generics and T in C#

Generics in the .NET Framwork allow you to work with generic types without having to specify a data type until the class is instantiated. Generics are useful, in that you may have a class that you don’t want to limit the functionality to a certain data type. For example, you may have a class that can compare 2 integers, or it could also compare 2 strings. Instead of creating 2 … Continue reading Generics and T in C#

Collections in C#

The .NET Framework contains different types of collections that can be used to store and retrieve data. In this post we will look at the different types. Collections consist of: Queue Stack ArrayList List LinkedList Dictionary Each collection type has different benefits. Collections in the .NET Framework are in: System.Collections System.Collections.Generic Queue A queue is a FIFO (first in first out) list. You can think of this as like a … Continue reading Collections in C#