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#

C# Interfaces

The purpose of Interfaces in C# is to define a contract. Any class that then implements the interface will know which methods it needs to have. An interface is like an abstract class, but without any implementation. Let’s look at an example. Let’s say we have a Vehicle interface, like below. A Vehicle will have wheels. If this were an abstract class, we could define the implementation of HowManyWheels, for … Continue reading C# Interfaces

C# Dictionaries

Dictionaries are used in C# to define key value pairs. They are part of System.Collections.Generic. The format is: Dictionary<key,value>   To define a dictionary, use the format: Dictionary<datatype, datatype> d = new Dictionary<datatype, datatype>(); For example: Dictionary<int, int> d = new Dictionary<int, int>(); or Dictionary<string, int> d = new Dictionary<string, int>(); To add to the data type, you can do either: Dictionary<string, int> d1 = new Dictionary<string, int>(); d1.Add(“Bob”, 12345); or d[3] = 30; From there, you can use different methods on the dictionary such as ContainsKey, ContainsValue: if (d.ContainsKey(2)) {     Console.WriteLine(d[2]); }  

Visual Studio Snippets

In Visual Studio, code snippets are available to help with development. To use code snippets, open a Visual Studio project. Type CTRL-K then X. You will see below: You can also enter shortcut keys. For example, enter ctor and tab twice to have a constructor created: Result: Entering class and tab twice will create a class: Other often used code snippets such as try/catch, loops, etc follow the same tab … Continue reading Visual Studio Snippets

Console App Exit Codes

Console apps can return exit codes to provide a way to show the app has successfully run. Here we will go through an example of creating an exe that returns an exit code. First, create a new C# console app: Now, to the main method, we will add: static void Main(string[] args) { Environment.ExitCode = 5; } The Environment.ExitCode will set the exit code value. To get the output, we … Continue reading Console App Exit Codes

Creating a Windows Bat File

A batch file in Windows is a way to automate Windows tasks. Batch files contain the extension .bat. To create a batch file, create a new file in a text editor such as Notepad, enter your commands, and save the file with a .bat extension, e.g. tasks.bat. Batch files are useful in that you don’t have to repeat typing a series of Windows commands such as running executables – they … Continue reading Creating a Windows Bat File

Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

This error occurs when trying to perform operations on a windows control when using threading. Here we will go through the error and how to resolve it. We will create a Windows Forms app in Visual Studio: First, we will create a working simple solution, without using threads. Add a label and a button: Add the code on the clicking of the button to set the label1 to “Hello World”: … Continue reading Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

C# Thread()

C# applications are often required to implement multithreaded functionality. Thanks to the .NET Framework, implementing threading is not difficult. In C# there are different ways we can create threads. Here we will look at using Thread. The Thread class is part of System.Threading. It is useful in that you can control many aspects of threads, such as whether to run in the foreground or background, the thread pool etc. We … Continue reading C# Thread()

C# Using the BackgroundWorker

The background worker in the .NET Framework is used to run background operations in a background thread. This is useful if you don’t want to tie up your user interface while an operation completes, such as downloading a large file. The background worker exposes events ProgressChanged and RunWorkerCompleted, which are useful to display progress to the user in a UI scenario. In this example, we will create a WPF form … Continue reading C# Using the BackgroundWorker

C# – Inheritance

Carrying on from our previous transport post, we currently have a car class that has some properties. However, a car is a type of vehicle. What if we wanted to expand to other types of vehicles in our solution? Let’s add a vehicle class. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Transportation {     class Vehicle     {     } } Now, as we shoudn’t create an object out of “vehicle”, we will define this as an abstract class: using System; using System.Collections.Generic; using System.Linq; … Continue reading C# – Inheritance