C# Wildcard Variables

Originally posted to: http://blog.simontimms.com/2016/11/09/c-wildcardsdiscardsignororators/

There is some great discussion going on about including discard variables in C#, possibly even for the C# 7 timeframe. It is so new that the name for them is still up in the air. In Haskel it is called a wildcard. I think this is a great feature which is found in other languages but isn’t well known for people who haven’t done funcitonal programming. The C# language has been sneaking into being a bit more functional over the last few releases. There is support for lambdas and there has been a bunch of work on immutability. Let’s take a walk through how wildcards works.

Let’s say that we have a function which has a number of output paramaters:

void DoSomething(out List<T> list, out int size){}

Ugh, already I hate this method. I’ve never liked the out syntax because it is wordy. To use this function you would have to do

List<T> list = null;
int size = 0;
DoSomething(out list, out size);

There is some hope for that syntax in C# 7 with what I would have called inline declaration of out variables but is being called “out variables”. The syntax would look like

DoSomething(out List<T> list, out int size);

This is obviously much nicer and you can read a bit more about it at
https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

However in my code base perhaps I don’t care about the size parameter. As it stands right now you still need to declare some variable to hold the size even if it never gets used. For one variable this isn’t a huge pain. I’ve taken to using the underscore to denote that I don’t care about some variable.

DoSomething(out List<T> list, out int _);
//make use of list never reference the _ variable

The issue comes when I have some funciton which takes many parameters I don’t care about.

DoSomething(out List<T> list, out int _, out float __, out decimal ___);
//make use of list never reference the _ variables

This is a huge bit of uglyness because we can’t overload the _ variable so we need to create a bunch more variables. It is even more so ugly if we’re using tuples and a deconstructing declaration (also part of C# 7). Our funciton could be changed to look like

(List<T>, int, float, decimal) DoSomething() {}

This is now a function which returns a tuple containing everything we previously had as out prameters. Then you can break this tuple up using a deconstructing declaration.

(List<T> list, int size, float fidelity, decimal cost) = DoSomething();

This will break up the tuple into the fields you actually want. Except you don’t care about size, fidelity and cost. With a wildcard we can write this as

(List<T> list, int _, float _, decimal _) = DoSomething();

This beauty of this wildcard is that we can use the same wildcard for each field an not worry about them in the least.

I’m really hopeful that this feature will make it to the next release.

The Monsters Weekly - Episode 77 - Internationalization Part 2 - Request Localization

Are you ready for more Internationalization? Monster Dave talks about the Request Localization Middleware in ASP.NET Core and the three built in RequestCultureProviders.

Links:

Internationalization Part 1

ASP.NET Core Docs

The Monsters Weekly - Episode 76 - Curl

Postman is a great tool for exercising HTTP endpoints but sometimes you need a tool you can run in a script or you don’t have access to postman. In that case curl is your friend. In this episode Simon takes us through the basics of curl running on Ubuntu on Windows. 

 

You can read more about curl at 

https://curl.haxx.se/docs/manpage.html

 

The Monsters Weekly - Episode 75 - Internationalization Part 1 - Localizing Text

In today’s episode, Monster Dave takes us through the IStringLocalizer and IViewLocalizer services used to support text localization in an ASP.NET Core MVC application.

Links:https://docs.asp.net/en/latest/fundamentals/localization.html

Be sure to check out the second episode in internationalization too: find it here.

The Monsters Weekly - Episode 74 - Customizing Bootstrap 4 in Your ASP.NET Core MVC Application

You want to treat your CSS like versioned code but still be able to take advantage of updates to CSS frameworks and libraries like Bootstrap 4. How do you make that work?

In this episode Monster James shows us how to frame up your project in such a way that you can customize variables, colors, sizes and even select which components are included in your custom version of Bootstrap, all the while maintaining an upgrade path and building from Bootstrap source.

Here’s some other episodes mentioned in the episode:

Yarn is the npm we need

The default package manager for node is called npm. Apparently it doesn’t stand for Node Package Manager but it sure seems like it should. Npm brags that they have have hundreds of millions of downloads per month. However that includes download from build servers and from package restores and doesn’t map to the number of people typing

npm install tsc

The Monsters Weekly - Episode 73 - Why Do I Need To Know What a Reverse Proxy Is?

For many developers, deployment may no longer be the same-old, same-old process of delivering an application to be hosted in IIS running on a Windows server. In fact, for any ASP.NET Core MVC application, your application will be running in Kestrel, but as a host Kestrel doesn’t support things like SSL termination, multiple port binding or multiple host headers? How are we going to make this work? And what is a reverse proxy?

Check out these previous episodes on nginx and kestrel:

Looking forward to your comments and questions!

The Monsters Weekly - Episode 72 - .NET Standard

.NET Standard is a new way of thinking about the API surface area of various different .NET frameworks and the platforms on which they run. Moving away from the confusion of portable class libraries .NET Standard allows for easier to comprehend libraries support matrices and a low friction approach to building libraries. 

 

References 

XKCD - Standards

Introducing .NET Standard

.NET Standard Platform

 

The Monsters Weekly - Episode 71 - Unit Testing with xUnit

Can you believe we’ve gone 70 episodes without talking about unit testing in ASP.NET Core? We’ve avoided it for as long as we could and it’s time to finally write some times. Join Monster Dave as he gives us a brief overview of xUnit and unit testing in ASP.NET Core!

Additional Resources:
Microsoft Docs on Unit Testing
xUnit Docs

The Monsters Weekly - Episode 70 - Accessing View Context from Tag Helpers

In some situations, it might be convenient to have access to some ViewData, ModelSate or even HttpContext information from within a Tag Helper. Join us as Monster Dave shows us how to do just that using ViewContext.