Showing posts with label .NET Core. Show all posts
Showing posts with label .NET Core. Show all posts

Friday, April 24, 2020

Iterate two arrays simultaneously with one foreach statement in C#


Let’s say that you have two lists or two arrays that you wish to combine.  One has a list of first names, the other a list of last names.  The list items are in the correct order so that the first name would match the last name for a given ordinal number.  But the lists may not have the same lengths (i.e. one list is not as complete as the other).  We wish to match all of the available first and last names until we run out of matching pairs.

So, what do we do?

For two arrays, we could use a “for int” loop.  We could get the size of each array by calling its Length property. And then we can compare which length is smaller; and use that number as our loop’s upper boundary.  We could then use the indexer to refer to each element in turn.  But this solution is rather inelegant.

For two lists, we should not use a “for int” loop, since lists do not have the concept of indexers.  We cannot refer to individual elements of the list via an index.  We can instead use a “foreach” loop.  But how do you refer to corresponding elements from 2 lists with each loop iteration?

This is where the Linq’s Zip method comes in very handy.  With the Zip method, we can apply a specific function to the corresponding elements of two sequences and produce a sequence of the results.

Using the above scenario as an example, where we have 5 first names but only 4 last names, we can write the following code:


The Output is below.

You will notice that the Zip function stopped after its 4th iteration.  In this way, we don’t have any incomplete names.

That’s it for now.  I hope this tip has been super helpful.



Monday, August 31, 2015

The type or namespace name 'Twilio' could not be found


So, you have finally joined the exciting new world of ASP.NET 5. 
Congratulations!  And welcome!

You may be running Visual Studio 2015 - or maybe Visual Studio Code - and creating new DNX projects and ASP.NET 5 applications that target the latest .NET Framework 4.6 or .NET Core 5, running on DNX, and compiling with RyuJIT or the Roslyn platform.
Good for you!

So, let's say that you writing a DNX project that makes use of the Twilio SMS API Client.
If you're reading this blog, then you probably already know that Twilio SMS is an API that lets you programmatically send, receive and track text messages globally.

Your code, in simple terms, will look like the one below


Your logic looks solid. 
You instantiate your Twilio client with your Account Sid and Auth Token, and you invoke the SendMessage service by passing in the appropriate parameters.

So why then, does your build fail and the Error List pane show the error below?


The type or namespace name 'Twilio' could not be found (are you missing a using directive or an assembly reference?)

How can this be? 
You already have the correct Using statement.
And you have already installed the Twilio package through NuGet.

Well, what's happening here can be explained by looking at your "project.json" file.


Here, we see that both the "dnx451" (.Net Framework) and "dnxcore50" (.Net Core 5) are being targeted.
However, .Net Core 5 does NOT support the Twilio SDK.  Ergo the "type or namespace" error.

There are a couple of ways to resolve this issue.
The first is to simply remove the "dnxcore50" from the list of targeted frameworks in the project.json file.


This is a simple and straightforward solution.
However, it does exclude the .NET Core 5 framework from your project altogether, and that may not be your intent.

The second alternative is something of a "shim". 
Using a compiler directive, you can exclude any code that "dnxcore50" does not support.



In this way, you are still targeting the .NET Core 5 framework, but at the same time,
your app does not break on any code that the framework does not support.

Happy .Net coding!