C# Code Snippets Reflection

C# Reflection

In this code snippet, we will take a look at reflection in C#. Reflection is used to get metadata(information) about an object at runtime. We can get members(properties, methods) of objects and their data types. Reflection is also used for late binding. The ability to see the metadata of an object is for example, useful is when you use generics, as you don’t necessarily know the data type of a generic member until the object is created.

C# Code Snippets Operator Overloading

C# Operator Overloading

In this code snippet, we will take a look at operator overloading in C#. Just like methods operators can be overloaded too. In the code below we have an example with geometric shapes. If we use the + operator on two objects of GeomentricShapes we get an error. This happens because the compiler doesn’t know what is supposed to happen when the  + operator is used on a GeomentricShapes object. We have to overload the + operator and write the code to be executed when two GeomentricShapes objects are added together. 

C# Code Snippets Attributes

C# Attributes

In this code snippet, we will take a look at attributes in C#. Attributes are used to add additional information(metadata) to code. For example, you can use [Serializable] to indicate that a class can be serialized. Or as I will demonstrate in the code example a method can be marked as obsolete and Visual Studio will warn you when you attempt to use the obsolete method. 

C# Code Snippets Preprocessor Directives

C# Preprocessor Directives

In this code snippet, we will take a look at data types in C#. Preprocessor directives are statements that get executed before compilation. They are designated by the # sign. Preprocessor directives can, for example, be useful when debugging code. Suppose we can make an if statement that checks whether the program is in debug mode. If so we would enable some diagnostic output otherwise we wouldn’t.

C# Code Snippets GUID

C# GUID

In this code snippet, we will take a look at GUIDs in C#. A GUID or a Globally Unique Identifier(also known as a Universally unique identifier )  is an alphanumeric sequence of character that make up a completely unique identifier(at least in theory). One place a GUID can be used is with databases to serve as a unique ID.

C# Code Snippets Constructor Chaining

C# Constructor Chaining

In this code snippet, we learn how to chain constructors in C#. Constructors are methods meaning you can overload them. Overloading can lead to code duplication. We can use chaining to get rid of the code duplication. Instead of implementing duplicate code in the overloaded method we can just call the previous method. This process is called chaining.

C# Code Snippets Overloaded Methods

C# Overloaded Methods

In this code snippet, we will learn how to overload methods in C#. Method overloading is when you make more than one method with the same name. The other method/methods must, however, have a different signature(different input parameters) so the compiler can differentiate between the versions. 

C# Code Snippets Operators

C# Operators

In this code snippet, we will take a look at all the operators present in C#. An operator is a symbol or a set of symbols that represent a specific function that will be preformed on the operands(our values or variables). There are multiple types of operators: arithmetic, logical, comparison … In this post, we will take a look at some of the most basic ones. Here is a complete list of all the operators and their functionalities. 

C# Code Snippets Constructors And Destructors

C# Constructors And Destructors

In this code snippet, we’ll learn about constructors in C#. A constructor is a method that gets called when a class is initialized. We can pass arguments into the constructor method and then use them inside the method to initialize the properties of the class. A class can have multiple constructors as they are methods and can thus be overloaded.

C# Code Snippets Variables

C# Variables

In this code snippet, we will learn how to use variables in C#. A variable is a container(aka a place in memory) that holds a value. The value can be any of the many data types in C#. To make a variable you must first declare its data type followed by the name you will use to reference that particular variable. Then you can assign it a value using the = assignment operator.

Advertisment ad adsense adlogger