Pivotal GemFire® Native Client v9.0

API Programming Example – C#

This C# programming code in the next example demonstrates how to use two or more clients sharing a distributed region in a Geode cache.

using System;
using GemStone.GemFire.Cache;

namespace GemStone.GemFire.Cache.QuickStart {
  // The BasicOperations QuickStart example.
  class BasicOperations {
      static void Main(string[] args) {
      try {
          // Create a Cache.
          CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(null);

          Cache cache = cacheFactory.SetSubscriptionEnabled(true).Create();

          Console.WriteLine("Created the Cache");

          RegionFactory regionFactory = 

          Region region = regionFactory.Create("exampleRegion");

          Console.WriteLine("Created the Region Programmatically.");

          // Put an Entry (Key and Value pair) into the Region using the 
          // direct/shortcut method.
          region.Put("Key1", "Value1");

          Console.WriteLine("Put the first Entry into the Region");

          // Put an Entry into the Region by manually creating a Key and 
          // a Value pair.
          CacheableInt32 key = new CacheableInt32(123);
          CacheableString value = new CacheableString("123");
          region.Put(key, value);

          Console.WriteLine("Put the second Entry into the Region");

          if (IntPtr.Size == 8) { // Are we a 64 bit process?
              Char ch = 'A';
              string text = new string(ch, 1024 * 1024);
              for (int item = 0; item < (5 * 1024 /* 5 GB */); item++) {
                  region.LocalPut(item, text);
              Console.WriteLine("Put over 4 GB data locally");
          // Get Entries back out of the Region.
          IGFSerializable result1 = region.Get("Key1");

          Console.WriteLine("Obtained the first Entry from the Region");

          IGFSerializable result2 = region.Get(key);

          Console.WriteLine("Obtained the second Entry from the Region");

          // Invalidate an Entry in the Region.

          Console.WriteLine("Invalidated the first Entry in the Region");

          // Destroy an Entry in the Region.

          Console.WriteLine("Destroyed the second Entry in the Region");

          // Close the GemFire Cache.

          Console.WriteLine("Closed the Cache");
      // An exception should not occur
      catch (GemFireException gfex) {
          Console.WriteLine("BasicOperations Exception: {0}",