How to do it...

  1. In your console application, add a new class called DotNet. To this class, add a property called AvailableDatatype:
        public class DotNet 
{
public string AvailableDatatype { get; set; }
}
  1. In the main program class, add a new static action event called types. Basically, this is just a delegate and will receive some value; in our case, the available .NET data types:
        class Program 
{
// Static action event
static event Action<string> types;

static void Main(string[] args)
{

}
}
  1. Inside void Main, create a List<DotNet> class called lstTypes. Inside this list, add several values of type DotNet class. Here, we will just add hardcoded data of some of the data types in .NET:
        List<DotNet> lstTypes = new List<DotNet>(); 
DotNet blnTypes = new DotNet();
blnTypes.AvailableDatatype = "bool";
lstTypes.Add(blnTypes);

DotNet strTypes = new DotNet();
strTypes.AvailableDatatype = "string";
lstTypes.Add(strTypes);

DotNet intTypes = new DotNet();
intTypes.AvailableDatatype = "int";
lstTypes.Add(intTypes);

DotNet decTypes = new DotNet();
decTypes.AvailableDatatype = "decimal";
lstTypes.Add(decTypes);
  1. Our next task is to subscribe to this event with an event handler that is simply outputting the value of x to the console window. We then raise the event each time we loop through our lstTypes list by adding the line types(lstTypes[i].AvailableDatatype);:
        types += x => 
{
Console.WriteLine(x);
};

for (int i = 0; i <= lstTypes.Count - 1; i++)
{
types(lstTypes[i].AvailableDatatype);
}

Console.ReadLine();
In reality, before raising an event we should always check that the event isn't null. Only after this check should we raise the event. For brevity, we have not added this check before raising the event.
  1. When you have added all the code from step 1 to step 4, your console application should look like this:
        class Program 
{
// Static action event
static event Action<string> types;

static void Main(string[] args)
{
List<DotNet> lstTypes = new List<DotNet>();
DotNet blnTypes = new DotNet();
blnTypes.AvailableDatatype = "bool";
lstTypes.Add(blnTypes);

DotNet strTypes = new DotNet();
strTypes.AvailableDatatype = "string";
lstTypes.Add(strTypes);

DotNet intTypes = new DotNet();
intTypes.AvailableDatatype = "int";
lstTypes.Add(intTypes);

DotNet decTypes = new DotNet();
decTypes.AvailableDatatype = "decimal";
lstTypes.Add(decTypes);

types += x =>
{
Console.WriteLine(x);
};

for (int i = 0; i <= lstTypes.Count - 1; i++)
{
types(lstTypes[i].AvailableDatatype);
}

Console.ReadLine();
}
}
  1. Running your application will set our list with values and then raise the event created to output the values of the list to the console window:
  1. Let's see the working of events using Rx. Add a static Subject of string. You might also need to add the System.Reactive.Subjects namespace to your project as Subjects live in this separate namespace:
        class Program 
{

static Subject<string> obsTypes = new Subject<string>();

static void Main(string[] args)
{

}
}
  1. After the code that created the list of DotNet, we used += to wire up an event handler. This time round, we will use Subscribe. This is the IObservable portion of the code. After you've added this, raise the event using the OnNext keyword. This is the IObserver portion of the code. Therefore, as we loop through our list, we will call OnNext to pump out the values to the subscribed IObservable interface:
        // IObservable 
obsTypes.Subscribe(x =>
{
Console.WriteLine(x);
});

// IObserver
for (int i = 0; i <= lstTypes.Count - 1; i++)
{
obsTypes.OnNext(lstTypes[i].AvailableDatatype);
}

Console.ReadLine();
  1. When you've completed adding all the code, your application should look like this:
        class Program 
{
static Subject<string> obsTypes = new Subject<string>();

static void Main(string[] args)
{
List<DotNet> lstTypes = new List<DotNet>();
DotNet blnTypes = new DotNet();
blnTypes.AvailableDatatype = "bool";
lstTypes.Add(blnTypes);

DotNet strTypes = new DotNet();
strTypes.AvailableDatatype = "string";
lstTypes.Add(strTypes);

DotNet intTypes = new DotNet();
intTypes.AvailableDatatype = "int";
lstTypes.Add(intTypes);

DotNet decTypes = new DotNet();
decTypes.AvailableDatatype = "decimal";
lstTypes.Add(decTypes);

// IObservable
obsTypes.Subscribe(x =>
{
Console.WriteLine(x);
});

// IObserver
for (int i = 0; i <= lstTypes.Count - 1; i++)
{
obsTypes.OnNext(lstTypes[i].AvailableDatatype);
}

Console.ReadLine();
}
}
  1. When you run your application, you will see the same items are output to the console window, as it did earlier.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87