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#

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]); }  

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

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