C# Code Snippets Indexers

C# Indexers

In this code snippet, we will learn how to implement indexers in C#. Indexers allow classes to be indexed just like arrays. To retrieve or set a value you just provide an index inside a set of square brackets just like you would for an array.

C# Code Snippets Ref Keyword

C# Ref Keyword

In this code snippet, we’ll what the ref keyword does in C#. Variables can be passed either by value or by reference. Usually, we do it by value meaning you pass just the value and not the variable itself. So, any changes made locally inside the method scope won’t be seen outside of the method scope. 

C# Code Snippets Exceptions

C# Exceptions

In this code snippet, we will about exceptions in C#. When an error occurs in an application an exception will be thrown. If it’s left unhandled the application will crash. Exceptions also get thrown if you try to access a resource on your computer that is unavailable. For example, a file that doesn’t exist or is already opened by another program.

C# Code Snippets Partial Classes

C# Partial Class

In this code snippet, we will found out what partial classes are in C#. If a class is marked with the partial keyword a class with the same name can be declared in another file. Both of the classes will now share everything between them and act as one. The partial keyword basically allows us to split up a class into different parts that can be put into different files.

C# Code Snippets Recursive File Iteration

C# Recursive File Iteration

In this code snippet, we will see how to recursively iterate through files and folders in C#. To recursively iterate through the file system we will first call the getFileNames() method and pass it the path of our directory as an input parameter. The method will first get and then print all the files in the provided directory. After that, it will get all the directories. If there are any the getFileNames() method will call itself for every directory and pass the path as the parameter, else the recursion will stop.

C# Code Snippets Object and Collection Initializers

C# Object and Collection Initializers

In this code snippet, we will learn how to use object and collection initializers in C#. Initializers let you set any accessible property at the point of creation of an object or collection(without having to invoke the constructor). This can make it easier and faster to create objects/collections. And by faster, I mean faster for you to implement as a programmer and not faster performance-wise.

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.

Advertisment ad adsense adlogger