C# Code Snippets Queue and Stack

C# Stacks And Queues

In this code snippet, we’ll take a look at queues and stacks in C#. Stacks and queues are similar to lists in the way that they don’t require their size to be declared on creation and elements can be dynamically added or removed. In fact, all of the above(queues, stacks, and lists) are part of the same collections namespace and they implement some of the same interfaces. 

C# Code Snippets List

C# List

In this code snippet, we’ll find out how to use lists in C#. Lists are similar to arrays, but unlike arrays, you can add and remove items from lists without having to destroy the entire list and making a new one. Also, lists have a lot of useful methods such as find(), exists(), sort(), …  You can use intellisense in Visual Studio to explore all the methods available.

C# Code Snippets Dictionary

C# Dictionary

In this code snippet, we’ll take a look at dictionaries in C#.
Dictionaries can hold a collection of objects and are similar to lists. But unlike lists, the objects stored in the dictionary are paired with a key. That key can be used to access the object in the collection.

C# Code Snippets Iteration

C# Iteration

In this code snippet, we’ll find out how to iterate with the for, foreach, while and do while loop. What loops do is execute the code in their curly brackets(multiple times, maybe only once or even never). The number of times that they will do that depends on the condition that will get evaluated at the beginning of each iteration. Usually, the condition consists of the maximum number of iterations and the counter variable which contains the number of iterations so far. The counter is an integer that gets increased at the end of the loop. This way we can keep track of how many iterations were performed. 

C# Unchecked

In this code snippet, we’ll find out what the unchecked keyword does in C#. The unchecked keyword is used to suppress arithmetic overflow exceptions. We will try to add two integers whose sum will exceed the size of the integer data type. This will cause an overflow to occur. The overflow would normally throw an exception, but if we wrap the expression with unchecked the exception will get suppressed and the integer will just overflow.

C# Code Snippets

C# Code Snippets

This post contains a collection of C# related things I learned and projects I did. The C# Code Snippets section is where you can find C# and .NET related topics like decision statements, Object-Oriented programming in C#, Threading, Events, Collections, … Meanwhile, under Algorithms and useful bits of code you will be able to find such things as sorting algorithms, quick how-tos, and other useful bits of code that don’t really fit anywhere else. And finally, there is the C# Project section that has my C# projects.

C# Code Snippets C# Struct

C# Struct

In this code snippet, we’ll make a struct in C#. A struct is like a lightweight class. You would use a struct for making a small object that doesn’t have a lot of members. If your struct grows too big(has too many properties/methods) you should make a class instead.

C# Code Snippets Sealed Keyword

C# Sealed Keyword

In this code snippet, we’ll take a look at the sealed keyword in C#.
In the following example, we will make a sealed class and a sealed method. Then we’ll try inheriting from the sealed class and overriding the sealed method. As you will be able to see we’ll get an error as sealed methods can’t be overridden and sealed classes can’t be inherited from.

Advertisment ad adsense adlogger