public static void TheProblemAgain() { var stack = new ObjectStack(); stack.Push("Hello World"); stack.Push(100); Console.WriteLine(100 * (int)stack.Pop()); Console.WriteLine(100 * (int)stack.Pop()); }
public static void TransmitData() { var initialCount = ObjectStack.Count; Console.WriteLine("Initial Stack Contents Count {0}", initialCount); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(Environment.NewLine); try { for (var i = 0; i < initialCount; i++) { //Simulating an Exception if (i == 4) { throw new IndexOutOfRangeException(); } Console.WriteLine("Initial Value {0} at position {1}", ObjectStack.Peek(), i); PushIntoBackUpStack(ObjectStack.Pop()); } } catch (IndexOutOfRangeException exception) { Console.WriteLine(exception); OnExceptionRestoreStack(); } catch (Exception exception) { Console.WriteLine(exception); OnExceptionRestoreStack(); } finally { Console.WriteLine("Count: {0}", ObjectStack.Count); } }
public void EndObject() { var obj = ObjectStack.Pop(); if (!IsObjectSameAsPreviousObject(obj)) { JsonWriter.WriteEndObject(); } }
public void InvalidCastExceptionTest() { // Now suppose we want a stack that stores just integers: ObjectStack stack = new ObjectStack(); // It's easy to make mistakes: stack.Push("s"); // Wrong type, but no error! int i = (int)stack.Pop(); // Downcast - runtime error! }
public void InvalidCastExceptionTest() { // Now suppose we want a stack that stores just integers: var intStack = new ObjectStack(); // It's easy to make mistakes: intStack.Push("s"); // Wrong type, but no error! Assert.Throws <System.InvalidCastException>(() => { _ = (int)intStack.Pop(); // Downcast - runtime error! }); }
static void Main(string[] args) { // create a new IntStack ObjectStack stack = new ObjectStack(); stack.Push(2); stack.Push("apple"); stack.Push(8); for (int i = 0; i < 3; i++) { Console.WriteLine("Pop value: {0}", stack.Pop()); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
private static void OnExceptionRestoreStack() { foreach (var i in BackUpList) { ObjectStack.Push(i); } var initialCount = ObjectStack.Count; Console.WriteLine(Environment.NewLine); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Restored Stack Contents Count {0}", ObjectStack.Count); Console.WriteLine("Contents from Restored Stack"); for (var i = 0; i < initialCount; i++) { Console.WriteLine("Restored value {0} , at Position {1}", ObjectStack.Pop(), i); } Console.WriteLine("Restored Stack To Initial State"); }
public static void Main() { string input; ObjectStack os = new ObjectStack(1); do { System.Console.WriteLine($"Length: {os.Size}"); System.Console.Write("Enter a string: "); input = Console.ReadLine(); if (input != "") { os.Push(input); } } while(input != ""); Console.WriteLine("\nPopping the stack!"); while (!os.Empty) { Console.WriteLine(os.Pop()); } }
/// <summary> /// 获取对象 /// </summary> /// <param name="cout"></param> /// <returns></returns> public T[] GetObject(int cout) { if (ObjectStack.Count == 0) { var p = new T[cout]; for (var i = 0; i < cout; i++) { p[i] = GetT(); if (GetObjectRunTime != null) { p[i] = GetObjectRunTime(p[i], this); } } return(p); } else { #if USEConcurrent var p = new T[cout]; var lpcout = ObjectStack.TryPopRange(p); if (lpcout < cout) { var x = cout - lpcout; var xp = new T[x]; for (var i = 0; i < x; i++) { xp[i] = GetT(); } Array.Copy(xp, 0, p, lpcout, x); } if (GetObjectRunTime != null) { for (var i = 0; i < p.Length; i++) { p[i] = GetObjectRunTime(p[i], this); } } return(p); #else #if lockStack == true lock (lockStack) { #endif T[] p = new T[cout]; for (int i = 0; i < cout; i++) { if (ObjectStack.Count > 0) { p[i] = ObjectStack.Pop(); if (p[i] == null) { p[i] = GetT(); } } else { p[i] = GetT(); } if (GetObjectRunTime != null) { p[i] = GetObjectRunTime(p[i], this); } } return(p); #if lockStack == true } #endif #endif } }
/// <summary> /// 获取对象 /// </summary> /// <returns></returns> public T GetObject() { if (ObjectStack.Count == 0) { var p = GetT(); if (GetObjectRunTime != null) { p = GetObjectRunTime(p, this); } I1++; return(p); } else { #if USEConcurrent T p; if (ObjectStack.TryPop(out p)) { if (GetObjectRunTime != null) { p = GetObjectRunTime(p, this); } I2++; return(p); } p = GetT(); if (GetObjectRunTime != null) { p = GetObjectRunTime(p, this); } return(p); #else T p; #if lockStack == true lock (lockStack) { #endif if (ObjectStack.Count > 0) { i2++; p = ObjectStack.Pop(); if (p == null) { p = GetT(); } if (GetObjectRunTime != null) { p = GetObjectRunTime(p, this); } return(p); } else { p = GetT(); if (GetObjectRunTime != null) { p = GetObjectRunTime(p, this); } return(p); } #if lockStack == true } #endif #endif } }