Virtual Methods

Virtual methods are methods of a base class that can be overridden by a method in a derived class to provide a different implementation of that method. For example, you have method A from Class A and Class B inherits from Read more

Lambda Expressions

Lambda expressions simplify the syntax of anonymous methods. For example, the program below used an anonymous method that will be assigned to a delegate. using System; namespace AnonymousMethod { public delegate void MessageDelegate(string message); public class Program { public static void Main() Read more

Extension Methods

Extension methods are methods that add functionality to .NET types or types that you have defined. For example, you can extend the System.String class by adding a Reverse method that reverses the string. The following program shows an example of an extension method. using System; namespace Read more

Null Conditional Operator

The Null Conditional Operator is used for easier checking of null values for every member in object property path. We use the ?operator after a member or a method call then followed by another nested member. Here is the syntax: Member?.Member2?….MemberN; Before the Read more

Anonymous Types

You can define anonymous types which is a great way to define temporary types used for data storage types. Suppose that you just want a class that can hold three values inside its properties. public class Sample { public int Property1 { Read more

Type Inference

Type inference allows a variable to infer the type being assigned to it. No more should a variable be strongly typed. C# offers the varkeyword for type inference. var myInt = 10; var myDouble = 5.67; var myString = “Hello”; The three Read more

Initializers

Initializers allow you to initialize values of properties within a class. If you have several properties for example, and you don’t want to define a constructor that will get all the values that will be supplied for those properties, then you Read more

Anonymous Methods

Anonymous methods are methods that are not actually defined, therefore, only suited for one-time use. Anonymous methods are targeted for delegates. The following shows the syntax of an anonymous method. delegate (parameters) { //Code for the anonymous method }; For example, Read more

Events

Events are behaviors or happenings that occur when the program is running. Events are often used in visual programming like windows and web forms. Some examples of events are clicking a mouse, typing a text in a text box, changing the Read more

Nullable Types

You can make Nullable Types such as int and double to allow null to be their values. Null values are only storable to reference types like string and other objects. C# allows you to transform value types so that they can handle null values. You can Read more

Generic Collections

We can define a generic collection which is a collection of any type that you specify. For example, we can use the List<T> class fro the System.Collections.Generics namespace. The List<T> can a collection of objects of T type. So List<int> for example, will be a collection of integer values. using Read more

Type Constraints

Generic code must work for every type of data. You can specify type constraints for a generic method or class which only allows type specified in the list of constraints. public class GenericClass<T> where T: constraint { //some code } Read more

Generic Classes

Generic classes are classes that accept type parameters so it’s encapsulated operations can be useful to any data type. Defining a generic class is as simple as defining a generic method. It has a similar syntax of using angle brackets Read more

Generic Methods

If you are creating multiple methods with the same functionality but varies on the data type that they will accept or contribute on their functionality, then you can use generic methods to save you some typing. A general structure of Read more

Generics

Generics are classes, methods, or interfaces that adapt their behaviors depending the data types assigned to them. For example, we can make a generic method that can accept any kinds of data. We can make one method showing int, double, or string values depending on the Read more

Iterators

An iterator is a block of code that supplies all the values to be used in a foreach loop.  A class that represents a collection can implement the System.Collections.IEnumerable interface. This interface requires an  implementation for the GetEnumerator()method  which returns an IEnumerator interface. The IEnumerator interface has a Current property which contains the Read more

Creating Dictionaries

You can also make a class that derives from DictionaryBase. This way, you can access each element by key (usually of type string) and they will return its associated value. When calling an item from a dictionary, we can call it by its Read more

Creating Your Own Collection

C# gives you the ability to create your own collection classes. For example, you can create a class that can contain multiple instances of another class. This class will have the features such as adding and removing new instances from Read more

C# ArrayList

The ArrayList class permits you to store values of various information varieties and additionally offers you the power to feature and take away components any time you would like. the subsequent examples show a straightforward practicality of the ArrayList class. Read more

Collections

We learned that arrays allow you to store multiple values with the same datatype. The arrays inherit the System.Array abstract class which provides methods and properties for simple data such as the length of the array. Ordinary arrays in C# have fixed length Read more

Viewing Values of Variables

When in Debug Mode, you can go to Code View to inspect values and states of objects and variables. You can use breakpoints to temporary suspend your program from running so you can examine your code. Visual Studio and Visual Read more

Stepping Through Your Code

Once you set up breakpoints, you can use certain commands to step through you code line by line. This allows you to carefully examine what each line of code does and how it affects the program. Create a new Console Read more

Breakpoints

Breakpoints allow your program to stop at certain points of the execution. You put breakpoints at a location of the code that you want to investigate. Breakpoints will solely be attack feasible lines of code. Once the program is suspended victimization Read more

User-Defined Exceptions

You can create your own exceptions. User-defined exceptions must inherit from the Exception base class. We will create another separate class that inherits from the Exception base class. Create a new Console Application and name it UserDefinedExceptions. After the project is created, click the Add Read more

Exception Properties

: The System.Exception base class is the class  that is inherited by all exception classes in the .NET Framework. Therefore, the  properties of the Exception class is available to  other exception classes. The following table shows some notable properties of  the System.Exception class which is shared Read more

Throwing Exceptions

You can throw exceptions anywhere in the program to artificially create an occurrence of an error. Additionally, you can create custom messages if you don’t like the default message of an exception. The following program shows you an example. using Read more

Using finally Blocks

Using finally blocks, you can provide a code that will always execute even if exceptions  are thrown. We learned that if an exception was thrown inside a try block, all the following codes in the try block will be skipped because the execution will immediately jump Read more

try and catch Statements

You can handle errors by using a try…catch statement. You wrap the code that you suspect to trigger an error inside a try block. The catch block contains the codes to be executed when an error is encountered. The following program demonstrates using try…catchstatements. using System; namespace Read more

Unhandled Exceptions

Unhandled exceptions are exceptions that were not properly handled by the program causing the program to terminate. We will demonstrate here what will happen if a program detects an exception during runtime and there is no exception handling used. You will Read more

String Interpolation

String interpolation or interpolated strings allows you to specify expressions right within a string literal for better readability. The string literal will serve as a template containing interpolation expressions that will be replaced with values that resulted in those expressions. Read more

Indexers

Indexers allow you to access an element of a private array or collection field. When you indicate an index for an instance of a class, indexers will get the right data indicated by the index. The following program shows an example Read more

Arrays for Classes

You can use arrays of objects. Creating arrays of objects is pretty much the same as when creating arrays for primitive data types such as int. For example, we can create an array of the Person class. using System; namespace ArrayOfObjectsDemo { public Read more

as Operator

You can use the as operator in C# to convert a class into another class within the same inheritance hierarchy. The as operator is equivalent to using casting with some minor differences as will be explained later. The following shows the syntax of using Read more

Polymorphism

Polymorphism allows classes to transform into a different class within its inheritance hierarchy. It allows a programmer to use objects of different types but treat them in their “general” form while maintaining their specific functionality. For example, there are many kinds Read more

Partial Classes

Partial classes allows you to define a class in multiple seperate files. For example, you can place all the fields, properties, and constructors inside a file, and place all the methods in another file. To define a class as partial, simply Read more

C# Sealed Classes

Sealed class are classes that cannot be inherited by other class. Since sealed-classes cannot be inherited, they cannot be abstract. The following program shows an example of a sealed-class. namespace SealedClassesDemo { public sealed class Base { private int someField; public Read more

Abstract Classes

Abstract classes are classes that always serves as base class for other classes. Abstract classes are slightly similar to interfaces but abstract classes can also have methods and properties that already have implementations. Abstract classes can also have constructors just like Read more

C# Interface

C# Interfaces are similar to classes, but it only contains declarations for methods and properties. Interfaces can be considered as plugins to classes. The class that implements a particular interface is required to make an implementation for the members and methods Read more

is Operator

The is operator in C# allows you to test if an object can be converted into another object mainly by using casting. The is operator requires two operands, the one that will be tested, and the type to which that object will be compared. Read more

Operator Overloading

Operator overloading allows you to customize the behaviors of C# operators depending on the type of its operands. Operator overloading allows a C# operator to interpret objects in a different way. Consider the example below. Example 1 – Not Taking Advantage Read more

Containment

Containment or composition is the process of adding another class as a member of a class. For example, a Person class can have a field of type Name. The program below shows an example of a class using containment. using System; namespace ContainmentDemo { Read more

Boxing and Unboxing

Boxing is the process of converting a value type such as a structure into an object or a reference type. Unboxing is the opposite the opposite process and converts reference types to value types.   Example 1 demonstrates boxing. namespace BoxingAndUnboxingDemo { struct MyStruct Read more

System.Object Class

All classes in the .NET Framework inherits from the System.Object class. The System.Object class is mapped to the object keyword in C#. For simplicity, I will just use object to refer to System.Object. The following is a list of some common methods of the objectclass. Method Return Type Virtual Static Read more

Virtual Methods

Virtual methods are methods of a base class that can be overridden by a method in a derived class to provide a different implementation of that method. For example, you have method A from Class A and Class B inherits from Read more

Static Members

The static keyword is used on members and properties that want to be shared by all instance of the class. When a method or property is defined as static, you can call them without creating an instance of that object. Let’s define some Read more

protected Access Specifier

The protected access specifier allows class members to be accessed  only by those who inherits from them. We learned about the public and private access specifiers. The protected access specifier is used when only the derived classes and the class itself should be able to access the members. Read more

Inheritance

Inheritance allows a class to absorb or inherit all the properties and methods of another class. It is like a parent-child relationship where the child inherits some properties from his/her parents such as facial features and attitudes. The base class or Read more

Class Libraries

You can create a Class Libraries which is a collection of classes and other codes that can be compiled and redistributed for software reusability. Class libraries are compiled into dynamic link libraries (DLL). DLLs cannot be read when you open it Read more

Structures Versus Classes

So what’s the difference between a class and a structure? Structures are value types like int, double, and string. When you copy a value of structures to other variables, it copies its values to the other variable and not its address or reference. Read more

Namespaces

Using a namespace is a way of packaging your application codes such as classes into a uniquely named structure. Everything in .NET Framework is wrap inside at least one namespace. When naming your class, there is a big chance that other Read more

Properties

Properties are the C# standard for accessing private data members inside a class. They may behave like normal fields, but properties have getters which gets the value of the private data and setters which sets a value to a private data. Getters and setters can directly retrieve Read more

Encapsulation

Encapsulation or information hiding is the process of hiding  the sensitive data of an object from a user and only providing him/her with what  is necessary. When we define a class, we often have states, which are the data  members that Read more

Access Specifiers

Access specifiers determine the accessibility of a class method or data members. We’ll take a look at the public and private access specifiers. The public access specifier is used when you want to access a method or data members outside the class or even outside the project. Read more

readonly Fields

The readonly  keyword is used for fields that will not allow modification of values. readonly  fields have similar characteristics as constants except that you can allow a readonly field to have no value from the declaration. Even though, you must assign values for readonly fields inside Read more

Destructors

Destructors are the opposite of constructors. Destructors are also special methods that are called when the object is being destroyed. Objects use the memory of your computer and if they are not removed from the memory, you will suffer a memory leak. You Read more

Constructors

Constructors are special methods that are required to create or “construct” objects. They allow you to assign the initial values for each data member of an array and add codes that you want to execute when you create an object. If Read more

Defining a Class

A C# class let’s you define a user-defined data type that contains fields, properties, and methods. A class serves as a blueprint for an object. The  object is the actual thing that follows the structure or possesses the properties and behaviors of the class. Read more

Structures

structures in C# or structs are user-defined data types that can contain other variables as its fields and methods as its behavior. You can create your very own customized data type using structures. Let say you want a data that don’t just Read more

Converting Enumerations

You can converting enumerations to other values and vice versa. Enumeration values are actually numeric values that are just masked by their names so that they can easily be understood. Let’s take a look at an example. using System; namespace Read more

Enumeration

Enumeration is a way of defining your own type that can accept a predefined set of values identified by names. For example, you want a variable that can only store directions such as east, west, north, and south. You can define Read more

Command Line Arguments

To successfully run a C# application, you must provide a Main method. The  Main() method is the starting point of the program. It is declared public or internal,  and it must also be static. You can actually pass command line arguments to the Main() method. This is  demonstrated by Read more

Delegates

Delegates are types that hold references to methods instead of variables. A delegate can copy the behavior of any method. To declare a delegate, you use the delegate keyword. Declaring a delegate is similar to declaring a method except that it has no Read more

Recursion

[print-me] Recursion is a process where a method calls itself repeatedly until it arrives on a desired value. Recursion is a complex topic in programming and mastering it is not that easy. Recursion must stop at some point or else, it Read more

Method Overloading

[print-me] Method overloading allows you to declare two methods with the same name but has different signatures or parameter set. The program will automatically detect which method you are calling by looking at the arguments you are passing to the method. Read more

Optional Parameters

[print-me] Optional parameters, as the name suggests, are optional and you have a choice whether to provide the argument or not. Optional parameters are assigned with default values. If you do not provide or give an argument corresponding to that Read more

Variable Scopes

[print-me] Variable Scope in C# have a scope. The scope tells where the variable is available or where in the program it can be accessed. For example, variables declared inside a method can only be accessed inside that method’s body. We Read more

C# params Keyword

The C# params keyword permits you to pass any variety of arguments that has an equivalent knowledge sort so store them into one single array parameter. The code below demonstrates the way to use the C# params keyword. using System; Read more

Arrays as Arguments

[print-me] You can pass arrays as arguments to a method. You have to specify the parameters of the method that it will accept an array. Let’s take a look at an example. using System; namespace ArraysAsArgumentsDemo1 { public class Program Read more

Out Parameters

C# Out parameters are parameters that can accept uninitialized variables or arguments. The out keyword is used when passing an uninitialized variable to a method. Uninitialized variables are variables that contain no value or it has not been assigned a value yet. The method Read more

Named Arguments

[print-me] Another way of passing arguments is by using named arguments. Named arguments free you from remembering or following the order of the parameters when passing arguments. In exchange, you have to memorize the names of the parameters of a method (but Read more

Parameters and Arguments

[print-me] Parameters give a method something to play with. They are like raw information that the method will process to give you the data you are looking for.  Imagine parameters as information you give to an employer that he needs in  order Read more

Methods

[print-me] Methods in c# let you define a behavior or task which composed of a set of codes that you can execute anywhere in the program. methods can accept arguments which supplement the method’s duty. You define a method inside a Read more

Jagged Arrays

[print-me] Jagged Arrays are multidimensional arrays containing rows of varying lengths. A simple multidimensional array is rectangular because it’s rows has the same number of columns, but jagged arrays can have different lengths for each of it’s rows. Therefore, jagged arrays Read more

Multidimensional array

Multidimensional array are arrays that use more than one index to access its content. Imagine a table with rows and columns, a multidimensional array can represent its data in a way a table can. Adding more indices increases the dimension of Read more

The foreach Loop

[print-me] The foreach loop is yet another looping structure in C# which is specially designed for arrays, lists, and collections.The foreach loop cycles through each of the elements, placing the element’s value into a temporary variable. You can access the element’s value through that Read more

C# Array

C# array is a kind of variable that stores a list of addresses of variables and can store a set of data having the same data type. Declaring multiple variables with the same type and used for the same purpose Read more

C# break and continue Statements

[print-me] C# break Sometimes, you want to stop the loop even if the condition is still true. How do you do that? Well, we can use the C# break keyword to stop the loop and the continue keyword to skip one loop cycle and continue to Read more

C# for Loop

C# for loop structure of C# is like an all-in-one looping utility. The C# for loop acts like the while loop only with some extra features. The syntax of a C# for loop is quite different compared to the other two looping structures. Read more

C# do while Loop

[print-me] The C# do while loop is another way of making your code repeat its execution. It is almost similar the while loop with one minor difference, it checks the condition after the code body has been executed. Let’s look at the Read more

The while Loop

[print-me] The C# while loop is the most basic looping structure in C#. It takes one condition and a set of codes that it will execute for as long as the condition remains true. The basic syntax of a while loop is Read more

Looping

[print-me] Looping allows you to repeat a set of code or statements while a given condition is true. Without it, you have to write multiple similar codes just to make your program repeat and that is tedious. Suppose you want to Read more

C# Switch Statement

C# offers switch statements which allow you to have multiple selections based on a constant value of a variable. A switch statement is equivalent to a multiple selections if statement but a switch statement is used if the variable to be compared has a constant value such as Read more

Nesting if Statements

[print-me] You can nesting if statements in C#. This is simply placing if statements inside if statements. You can make something as complex as the code that follows. if (condition) { code to execute; if (condition) { code to execute; } else if (condition) { if Read more

c# ternary operator

[print-me] The c# ternary operator (?:) in C# does the same thing as the if else statement though it is more suited on simple statements such as assigning two different values depending on a specified condition. Below shows the syntax of using Read more

The if Statement

[print-me] c# if statement-A program cannot think if it cannot make a decision. C# allows you to execute codes if certain conditions are true or false. By using an ifstatement and specifying the condition that will execute the code, you can Read more

Making Decisions

[print-me] C# control statements-Almost every programming languages allows you to execute a code when a certain condition is met. This adds logic to your program. Imagine a program that cannot make decisions and execute every line of code. That would Read more

C# Operator Precedence

[print-me] C# Operator precedence determines which to calculate first in an expression involving more than two operands. Operators in C# has their own priority during calculation, and the compiler calculates those with the highest priority first, following the next ones in Read more

Bitwise Operators

bitwise operators allow you to manipulate different kinds of data in their binary form. In order to follow this lesson, it is recommended that you know what binary system is and how to convert decimal numbers to binary. The binary Read more