Sponsored

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, April 15, 2021

dotPeek: a Totally Free Alternative for .NET Reflector

As a professional .NET developer you recognize the value of a .NET decompiler. There are many scenarios when the original source code is not available but requires an understanding and using a decompiler is definitely one of the best ways to look at the source code. One of the advantages working with a managed framework like .NET is an ability for a decompiler to convert an assembly into an code of a high level programming language like C# instead of a machine code. And this is exactly an expertise in which the dotPeek shines.

Saturday, December 26, 2020

C#/.NET: Validate Anonymous Objects During Unit Testing

Say, we have developed a REST API based on .NET Web API framework and need to write a unit test for an end point that returns a collection of objects. This sounds like a usual trivial task that many developers do all the time. Unless, the API method that we need to test, returns a collection of anonymous objects, that is the API method is written using a C# anonymous or dynamic type to construct objects that are included in the list.

It's totally fine when the endpoint is consumed over HTTP as the result is first serialized into JSON for transfer and then de-serialized back into a JavaScript object on a consumer side. If, however, the endpoint is called directly in C# without JSON conversion, like in a unit test, it's an entirely different story. Let's discuss how we can overcome this issue.

Friday, November 20, 2020

Reuse a MemoryStream without Writing to a FileStream

MemoryStream is a very useful class as it allows working with a Stream-like data in memory without having dependencies on any external resources like files, etc. Even though the MemoryStream implements an IDisposable interface it does not actually have any critical resources to dispose of, so, explicitly disposing of a MemoryStream object is not strictly necessary as the .NET memory management will take care of it. This specifics presents an opportunity of reusing the MemoryStream object if needed across multiple code scopes.

Saturday, November 5, 2011

Using Anonymous Types Outside of Local Context with Dynamic Keyword

Anonymous types that have been introduced in C# with .NET 3.5 is a convenient and powerful feature and can be used to simplify and speed up development without sacrificing code quality or violating coding standards.

Thursday, May 22, 2008

#if DEBUG in ASP.NET 2.0 code behind

If you use an ASP.NET web site in your solution (another alternative is WAP - web application project) you may have noticed that there is no more compiler options in Configuration Manager that would have allowed you to add compiler constants like DEBUG. So the question pops up whether it's still possible to use very convenient way of excluding debug code from the production version by using #if preprocessor directive. Luckily it turns out that it's still supported and now it's related to a value of a "debug" attribute of a "compilation" element of the Web.config file. If you surround a part of your code with

#if DEBUG
...
#endif

it will be included when debug="true" and excluded when debug="false".