Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET Rx- Where is System.Reactive?

I have an intensive Java background so forgive me if I'm overlooking something obvious in C#, but my research is getting me nowhere. I am trying to use the reactive Rx .NET library. The compiler is not complaining about the IObservable but it is with the call to the zip method. It is throwing the "... are you missing a using directive or assembly reference?"

I've been going through the namespaces and I cannot find what is looking for. I cannot find the System.Reactive which also throws an error if used, and all the references are already included for this Windows 8.1 app. Can someone please give me a lead on what is wrong?

public sealed class EventEngine {         private static readonly EventEngine singleton = new EventEngine();      public static EventEngine get()     {         return singleton;     }      public IObservable<MusicNote> CurrentKey { get; set; }     public IObservable<Scale> CurrentScale { get; set; }      public IObservable<AppliedScale> CurrentAppliedScale     {         get         {             return CurrentScale.zip(CurrentKey,                 (s, k) => AppliedScale.getAppliedScale(k, s));         }      }      private EventEngine() {} } 

*UPDATE *

Here is the working version after considering input from answers.

public sealed class EventEngine {     private static readonly EventEngine singleton = new EventEngine();      public static EventEngine get()     {         return singleton;     }      public IObservable<MusicNote> CurrentKey { get; set; }     public IObservable<Scale> CurrentScale { get; set; }      public IObservable<AppliedScale> CurrentAppliedScale     {         get         {             return Observable.Zip(CurrentScale, CurrentKey,                 (s, k) => AppliedScale.getAppliedScale(s,k));         }      }      private EventEngine() {} } 
like image 474
tmn Avatar asked May 07 '15 21:05

tmn


1 Answers

You have probably not added the necessary assembly references for Rx to your project. (Referencing an assembly is not the same thing as importing a namespace! You already know what a namespace is; an assembly is something similar to a JAR; the smallest unit of code deployment/distribution. Your project must reference it before the namespaces defined inside it become available for use.)

The compiler likely doesn't complain about IObservable<T> and IObserver<T> because your project is targeting .NET Framework version 4 or later. These two interfaces have been part of the core .NET Framework Class Library (FCL) since .NET version 4. (If you targeted an earlier .NET version, you'd get errors for using these undefined interfaces, too.)

Every part of Rx other than these two interfaces is not included in the core .NET FCL, but resides in their own (add-on) assemblies. You can add them to your project e.g. by installing the corresponding NuGet packages:

  1. In Visual Studio, go to ToolsNuGet Package ManagerPackage Manager Console.

  2. In the NuGet console window, select the target project (where you want to use Rx) in the drop-down list Default project.

  3. Next, type Install-Package System.Reactive and hit Enter ↵. (Note: This package used to be called Rx-Main previously; see this answer for details.)

  4. This will add the System.Reactive.* assembly references to your project.

like image 165
stakx - no longer contributing Avatar answered Sep 27 '22 22:09

stakx - no longer contributing