Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

C#

Articles on C# programming language.

C# Anonymous Function Handler

Fig 3. Anonymous Delegate Handler Function

Let us say, we need to define a handler for the event and do not want to define a handler function in the class definition. In that case, one can go for the Anonymous Function Handler. We can define Anonymous Function Handler either using a Lambda Expression or using the Anonymous Delegate. In this example, we will try both the technique.

Continue Reading →

Conversion Operator Overloading in C#

Fig 6. Using C# Overloaded Conversion Operators

Conversion Operator in C# converts one Type to another type. There are two flavours to it and we should implement both. One is Implicit Conversion and other one is Explicit Conversion. In a class type, we can provide two conversion operators those tells how a class can be casted to other type & how other type can be converted to class type. In this example, we will create a class called TimePart and add a conversion method to convert that to a String. We will also provide a conversion method which converts the String into a TimePart instance.

Continue Reading →

Operator Overloading in C#

Fig 2. C# Class With Overloading + Operator

We can overload an operator using the operator keyword followed by the symbol for the operator. In this example, we will overload an + operator to append comma with the strings. Since + is a binary operator, we need two instances of the string. In this example, we will write a class called CommaStr and have a static method with name ‘+’ which acts as an overloaded operator function. Unlike C++, Operator Overloading can be done for very specific operators in C#. You can refer MSDN for list of operators that can support overloading.

Continue Reading →

Dynamic ExpandoObject in C#

Fig 1. Create Expando Object

In the previous article, we learned about Anonymous Type to define properties at runtime. But these properties are read only and the type will not allow changing it after creation. C# provides a class called DynamicExpando which will also allow adding the properties at runtime. But we can read and write the property values at runtime with this object. Since the type allow adding the members on the fly, the compiler will not perform any type checking. We can also…

Continue Reading →