Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Contents tagged with C#

  • Log message Request and Response in ASP.NET WebAPI

    By logging both incoming and outgoing messages for services can be useful in many scenarios, such as debugging, tracing, inspection and helping customers with request problems etc.  I have a customer that need to have both incoming and outgoing messages to be logged. They use the information to see strange behaviors and also to help customers when they call in  for help (They can by looking in the log see if the customers sends in data in a wrong or strange way).

  • .NET 4.0 is so Lazy

    With .NET 4.0 there is a new class added to the System namespace called Lazy<T>. This class is what the name says, lazy. Here is an example where Lazy is used:

  • Is it important to write good code?

    The last three weeks I have visit several companies and talked about writing good code. It's amazing to see how different developer thinks about writing good code. Here are some comments when I asked if it's important to write good code:

  • Region is an excuse for hiding large files

    Often when I take a look at sample code there are a lot of regions. I need to open them all the time, it really start to make me crazy. Why does people even use Region in their code, what's the point of hiding code? I think it's only an excuse to hide code because of a too large file. What do you think?

  • Map Objects to one Object

    In my previous post I wrote about how we could use Dynamic Compilation for mapping object to object in a configuration file, mosly for fun to simply try something different. I spend a few minutes to see if I could do something more with it. It ended up that I can use it to several things, like creating Factories, or send in different kind of objects and map several into one. Here is how I for example can send in more domain object to map them into a PresentationCustomer:

  • Remove code smell with AOP

    During the last years you have probably heard about Aspect Oriented Programming (AOP). Different people like or don’t like AOP. I’m one of them who like AOP. As you may now VB.Net and C# are object orientated languages and with object oriented programming (OOP) you want to reduce code duplication, code duplications smells badly. If you need to do code duplication you are probably doing something wrong with the implementation of your domain model. With OOP you can generally eliminating code duplication, but there are some cases where we can’t avoid it, for example take a look at this code:

  • Multiple return values, I want this in C#

    I so badly want to have support for multiple return values in C# instead of using out parameters.

    Note: The following is only an example, we can use a struct or an object to create a data structure which will hold the value, but I only use this code for demonstration purpose.

    int x,y,z;
    bool isHidden;

    GetCords("MyElement", out x, out y, out z, out isHidden);


    Instead of the code above, I want to do something like this:

    Return 10,11,10,true;

    var x,y,z,isHidden = GetCords("MyElement");

    If I don’t care about the y and z return values I can instead write:

    var x,,,isHidden = GetCords("MyElement");

    Isn’t this beautiful?

    hmm,  when I’m thinking of beauty, I start to think about "The beauty is in the eye of the beholder" ;)