// Define an actor class that receives messages of type string and integer public class MyActor : ReceiveActor { public MyActor() { Receive(s => Console.WriteLine(s)); Receive (i => Console.WriteLine(i)); } } // Create an instance of the actor and send it some messages var system = ActorSystem.Create("MySystem"); var actor = system.ActorOf ("MyActor"); actor.Tell("Hello world!"); actor.Tell(42);
// Define an actor class that spawns child actors public class ParentActor : ReceiveActor { public ParentActor() { ReceiveThis code example shows how actors can spawn child actors, and how messages can be passed between actors. The ParentActor class creates new child actors when it receives a message, and the ChildActor class receives messages of type string and logs them to the console. The package library used in these examples is Akka.NET, which is a port of the original Akka library from Java to .NET/C#. Akka.NET provides a comprehensive actor model implementation for building scalable, distributed, and fault-tolerant systems.(s => { var child = Context.ActorOf (); child.Tell(s); }); } } // Define a child actor class that receives messages of type string and logs them public class ChildActor : ReceiveActor { public ChildActor() { Receive (s => Console.WriteLine(s)); } } // Create an instance of the parent actor and send it a message to spawn a child actor var system = ActorSystem.Create("MySystem"); var parent = system.ActorOf ("ParentActor"); parent.Tell("Spawn a child actor!");