C# Code Snippets Recursion

C# Recursion

In this code snippet, we will find out how to use recursion in C#. A recursive function/method is a method that calls itself. The method will contain an if else block. If the condition is met a value will be returned else the method will continue calling itself.

C# Code Snippets Enumerations

C# Enumerations

In this code snippet, we will see how to use enumerations in C#.
An enum is a data type. To create an enumeration you have to give it a name and define a list of constants that will be available when that enumeration is referenced.

C# Code Snippets Method Output Parameters

C# Method Output Parameters

In this code snippet, will see how to use method output parameters in C#. The output parameter works by passing in a reference of the variable you provide. It is very similar to the ref keyword. You put the out keyword in front of the parameter you want to use as an “output”. The method can still have a data type and return a value as the output parameter isn’t really outputting anything but is rather accessing the variable you passed it via reference.

C# Code Snippets Anonymous Methods

C# Anonymous Methods

In this code snippet, we’ll take a look at anonymous methods in C#. Anonymous methods don’t have names(obviously). They can be defined with the delegate keyword or by using the lambda expression. They can then be put into a variable of type delegate.

C# Code Snippets Abstract Keyword

C# Abstract Keyword

In this code snippet, we’ll find out what the abstract keyword does in C#. Classes and their members such as methods, properties, … that are marked with abstract keyword aren’t fully implemented. Abstract classes aren’t meant to be instantiated but are only meant to be inherited from. This means that class members(methods, properties, …) marked with the abstract keyword must be implemented in the child class.

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.

Advertisment ad adsense adlogger