C# Casting and Type Conversion

C# is statically-typed at compile time, after a variable is declared, it cannot be declared again or assigned a value of another type unless that type is implicitly convertible to the variable’s type.

For example, the string cannot be implicitly converted to int. Therefore, after you declare i as an int, you cannot assign the string “Hello” to it.

Implicit Conversion

No special syntax is required because the conversion is type safe and no data will be lost.

Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

Explicit Conversion

Explicit conversions require the cast operator (). Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons. Typical examples include numeric conversion to a type that has less precision or a smaller range, and conversion of a base-class instance to a derived class.

Continue reading

C# Boxing and Unboxing

When the CLR boxes a value type, it wraps the value inside a System.Object instance and stores it on the managed heap.

Boxing is implicit; Unboxing is explicit.


Boxing and Unboxing are computationally expensive processes.

When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally.


Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.

Boxing is used to store value types in the garbage-collected heap. Boxing a value type allocates an object instance on the heap and copies the value into the new object.


Unboxing extracts the value type from the object.

Continue reading

Four Basic Principles of Object-oriented Programming(OOP), Abstraction, Encapsulation, Polymorphism, Inheritance

I am sorry about next three paragraphs. Because, I wrote too much. If you don’t have time, you can skip them directly 🙂

The four principles of OOP are entry point to build a well designed OOP applications. You definetely need to know whether you are a junior or senior developer to write an effective code. Before telling about them, I just want to tell why you need to know them. Probably, you already use some of them even without knowing principles. But, when you know them, you will have a higher perspective and more confidence related with complex codebase(Projects). Because, writing a class is not difficult at the beginnig but when you have more complex codebase, it will be difficult to maintain development if you don’t apply some patterns and principles. Furthermore, it is a common language between software engineers. So, easy to communicate and easy to design complex applications. They are also not dependent to any programming language like Java, C#, Python etc. If you know and understand what they mean, then you can definetely benefit from them to build an application. By the way, some developers named as different naming convention like basic principles of OOP, main concepts of OOP, basics of OOP, basic concepts of OOP, etc 🙂 But all of them are mentioning about same things Abstraction, Encapsulation, Inheritance and Polymorphism.

Novadays, developers have to be very pragmatist because of the market. They need to learn and use new stuffs very quickly. For example, I read every time about new frameworks. Although I don’t develop any project with new framework, I spend my time to become ready on the software market. So, it is difficult to work and think on some basic concepts due to lock of time. But, I realized that I spend too much effort for every new framework or library to understand. When I anlyze different open-source projects for weeks, I had my way of exploring the projects in a time. This means that I want to follow same way every time unconsciously. It is very very funny 🙂 because I tried to create basic principles and patterns in my way unnecesarily. I every time try to find some key points. But, there is more funny side as well. Developers of open source projects were already applying basic principles. That is why, many developers could work together. In this case, I have learned many principles and patterns unconsciously. Finally, I learned the existence of basic concepts or principles of OOP :))))

Unfortunately I was not only one person who makes same mistakes in the market. Developers who have different experience still don’t know or care for significance of OOP principles. This doesn’t mean that they are not good developers. Many of them were developing complex projects with well known frameworks like Spring Boot, Express Framework, etc. But, they just copy and paste codes from tutorials generally. Big frameworks are making development easer and safer. So, we don’t need to think for low level details for our applications and just need to think about business logic. So, it is decreasing development cost and increasing efficiency for the organizations(companies). But, in this case, developers are becoming lazy in a time and don’t want to spend time to learn or even remember about basic principles of OOP and some common software patterns. Anyway, I decided to write this post for myself. I hope that some developers will benefit in the future as well 🙂

There are four basic principles of OOP as below;

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance

I read many different posts about them so I will write different versions of definitions that I took note and you can decide which one is more clear to you 🙂 They are not my sentences. I don’t want to create a new one. I just want to show the way of my thinking.

Continue reading

C# Pipeline Handler & Logger

I have created a demo project for log handler and TryCatch wrapper (http://kenanhancer.com/2018/06/26/c-trycatchwrapper-and-logging/_.

I need a logger which can write to console and file at the same time. So, I created this demo which creates logger pipeline dynamicly. When code runs, it will write message in console and FileLog.log file at the same time.

C# LocalDataStoreSlot, ThreadStatic, and ThreadLocal To Pass Thread Specific Data

Every thread will have isolated data. I try to show that concurrent web requests are coming to Controller classes and different data is passed to the threads. Lastly, don’t use with async/await, it is not working properly.




C# Asynchronous Programming Patterns

There are 3 Asynchronous Programming Patterns in C#.

1- Asynchronous Programming Model (APM)
2- Event-based Asynchronous Pattern (EAP)
3- Task-based Asynchronous Pattern (TAP)


1- Asynchronous Programming Model (APM)

Asynchronous Programming Model using Delegates

you can reach that article from Calling synchronous methods asynchronously

2- Event-based Asynchronous Pattern (EAP)

3- Task-based Asynchronous Pattern (TAP)

Chain of Responsibility Pattern and Aspect Oriented Programming with C#.NET

Each class contains only the business logic code, while the aspects will be responsible of intercepting the code in order to inject the cross-cutting concerns.

This is first version of example code.

This is second version. You can find MethodInfo and parameter values in the aspect classes.