How Yield keyword works in C#

Yield in C# , can use with the return statement to build a collection of returns. For example , I have a method that we need to return a collection of integers in a lazy manner and expose it through an IEnumerable interface. Based on the value produce with the yield return , consumer can write some logic based on their need.

In the below example you can see I have a method ReturnSomeIntegers() , which return 1 to 10 by iterate using for loop with yield keyword and also two more yield return with 20 and 30 respectively.

class Program
{
static void Main(string[] args)
{

YieldDemo();
Console.ReadKey();

}
static void YieldDemo()
{
foreach ( int n in ReturnSomeIntegers())
{
if (n == 20)
{
Console.WriteLine("From Yield Return Two - " + n);
}
else if (n == 30)
{
Console.WriteLine("From Yield Return Three - " + n);
}
else
{
Console.WriteLine("From Yield Return One - " + n);
}
}
}
private static IEnumerable ReturnSomeIntegers()
{
for(int i = 1; i <= 10; i++)
{
yield return i;
}
yield return 20;
yield return 30;
}
}

Whenever ReturnSomeIntegers() method return a single value , the execution will switchback to YieldDemo() for each method and print the value based on the condition.

In Computer Science this is known as Continuation
https://en.wikipedia.org/wiki/Continuation

Publish and Consume Azure Service bus queues pro-grammatically in C#

You can easily publish messages in to the queue and subscribe messages from queue pro grammatically using C#
Publish messages in to queues

var connectionString = "Endpoint=sb://rack-messaging.servicebus.windows.net/;SharedAccessKeyName=messagecollectionlistener;SharedAccessKey=DnentJvFthm13IWicMJxrM68LK3c+zEeQb6QlQn71SA=;EntityPath=messagecollection";
var client = QueueClient.CreateFromConnectionString(connectionString);
var lastKey = ' ';
var count = 1;
while (lastKey != 'q')
{
var body = $"Message {count++}";
var message = new BrokeredMessage(body);
client.Send(message);
Console.WriteLine($"Sent {message.MessageId}");
lastKey = Console.ReadKey().KeyChar;
}

Consume

var connectionString = "Endpoint=sb://rack-messaging.servicebus.windows.net/;SharedAccessKeyName=messagecollectionlistener;SharedAccessKey=DnentJvFthm13IWicMJxrM68LK3c+zEeQb6QlQn71SA=;EntityPath=messagecollection";
var client = QueueClient.CreateFromConnectionString(connectionString);
client.OnMessage(message => {
Console.WriteLine($"Message : {message.MessageId} Message Body : {message.GetBody()}");
});
Console.ReadLine();

Connection string you can copy from here

Theil’s Inequality Algorithm using C#

TheilsInequality

public class TheilInEqualityAlgorithm
{
private readonly int _holdOutPeriod;
private readonly IEnumerable _actualAndPredictedValues;
public TheilInEqualityAlgorithm(int holdOutPeriod, IEnumerable actualAndPredictedValues)
{
_holdOutPeriod = holdOutPeriod;
_actualAndPredictedValues = actualAndPredictedValues;
}
private double CalculateRootMeanSquareDeviation()
{
double sum = 0;
foreach (ActualAndPredictedValue actualAndPredictedValue in _actualAndPredictedValues)
{
double difference = actualAndPredictedValue.Actual - actualAndPredictedValue.Predicted;
double squareOfdifference = Math.Pow(difference, 2);
sum = sum + squareOfdifference;
}

double rootMeanSquareDeviation = Math.Sqrt(sum / _holdOutPeriod);
return rootMeanSquareDeviation;
}
public Dictionary<string,double> CalculateTheilInEquality()
{
var results=new Dictionary<string, double>();
double calculatedRootMeanSquareDeviation = CalculateRootMeanSquareDeviation();
results.Add("RMSD",calculatedRootMeanSquareDeviation);
double actualSquareSum = 0;
double predictedSquareSum = 0;
foreach (ActualAndPredictedValue actualAndPredictedValue in _actualAndPredictedValues)
{
double actualSquare = Math.Pow(actualAndPredictedValue.Actual, 2);
double predictedSquare = Math.Pow(actualAndPredictedValue.Predicted, 2);
actualSquareSum = actualSquareSum + actualSquare;
predictedSquareSum = predictedSquareSum + predictedSquare;
}
double sqrtOfActualSum = Math.Sqrt(actualSquareSum / _holdOutPeriod);
double sqrtOfPredictedSum = Math.Sqrt(predictedSquareSum / _holdOutPeriod);
double theilInEquality = (calculatedRootMeanSquareDeviation) / (sqrtOfActualSum + sqrtOfPredictedSum);
results.Add("TheilInEquality", theilInEquality);
return results;
}
}
public class ActualAndPredictedValue
{
public double Actual { get; set; }
public double Predicted { get; set; }
}

Pass actual and predicted values for the periods

var actualPredictedCollection = new List
{
new ActualAndPredictedValue
{
Actual =-659505.150000,
Predicted =-900587.246911
}};
var results = new TheilInEqualityAlgorithm(12,actualPredictedCollection).CalculateTheilInEquality();
double theilInEquality=results[“TheilInEquality”];
Enjoy coding…