當前位置: 首頁>>代碼示例>>C#>>正文


C# InvalidOperationException類代碼示例

本文整理匯總了C#中System.InvalidOperationException的典型用法代碼示例。如果您正苦於以下問題:C# InvalidOperationException類的具體用法?C# InvalidOperationException怎麽用?C# InvalidOperationException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


InvalidOperationException類屬於System命名空間,在下文中一共展示了InvalidOperationException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: threadExampleBtn_Click

private async void threadExampleBtn_Click(object sender, RoutedEventArgs e)
{
    textBox1.Text = String.Empty;

    textBox1.Text = "Simulating work on UI thread.\n";
    DoSomeWork(20);
    textBox1.Text += "Work completed...\n";

    textBox1.Text += "Simulating work on non-UI thread.\n";
    await Task.Run( () => DoSomeWork(1000));
    textBox1.Text += "Work completed...\n";
}

private async void DoSomeWork(int milliseconds)
{
    // Simulate work.
    await Task.Delay(milliseconds);

    // Report completion.
    var msg = String.Format("Some work completed in {0} ms.\n", milliseconds);
    textBox1.Text += msg;
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:22,代碼來源:InvalidOperationException

示例2: DoSomeWork

private async void DoSomeWork(int milliseconds)
{
    // Simulate work.
    await Task.Delay(milliseconds);

    // Report completion.
    bool uiAccess = textBox1.Dispatcher.CheckAccess();
    String msg = String.Format("Some work completed in {0} ms. on {1}UI thread\n",
                               milliseconds, uiAccess ? String.Empty : "non-");
    if (uiAccess)
        textBox1.Text += msg;
    else
        textBox1.Dispatcher.Invoke(() => { textBox1.Text += msg; });
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:InvalidOperationException

示例3: DoSomeWork

private async void DoSomeWork(int milliseconds)
{
    // Simulate work.
    await Task.Delay(milliseconds);

    // Report completion.
    bool uiAccess = textBox1.Dispatcher.HasThreadAccess;
    String msg = String.Format("Some work completed in {0} ms. on {1}UI thread\n",
                               milliseconds, uiAccess ? String.Empty : "non-");
    if (uiAccess)
        textBox1.Text += msg;
    else
        await textBox1.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { textBox1.Text += msg; });
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:InvalidOperationException

示例4: threadExampleBtn_Click

List<String> lines = new List<String>();

private async void threadExampleBtn_Click(object sender, EventArgs e)
{
    textBox1.Text = String.Empty;
    lines.Clear();

    lines.Add("Simulating work on UI thread.");
    textBox1.Lines = lines.ToArray();
    DoSomeWork(20);

    lines.Add("Simulating work on non-UI thread.");
    textBox1.Lines = lines.ToArray();
    await Task.Run(() => DoSomeWork(1000));

    lines.Add("ThreadsExampleBtn_Click completes. ");
    textBox1.Lines = lines.ToArray();
}

private async void DoSomeWork(int milliseconds)
{
    // simulate work
    await Task.Delay(milliseconds);

    // report completion
    lines.Add(String.Format("Some work completed in {0} ms on UI thread.", milliseconds));
    textBox1.Lines = lines.ToArray();
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:28,代碼來源:InvalidOperationException

示例5: DoSomeWork

private async void DoSomeWork(int milliseconds)
{
    // simulate work
    await Task.Delay(milliseconds);

    // Report completion.
    bool uiMarshal = textBox1.InvokeRequired;
    String msg = String.Format("Some work completed in {0} ms. on {1}UI thread\n",
                               milliseconds, uiMarshal ? String.Empty : "non-");
    lines.Add(msg);

    if (uiMarshal) {
        textBox1.Invoke(new Action(() => { textBox1.Lines = lines.ToArray(); }));
    }
    else {
        textBox1.Lines = lines.ToArray();
    }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:18,代碼來源:InvalidOperationException

示例6: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      var numbers = new List<int>() { 1, 2, 3, 4, 5 };
      foreach (var number in numbers) {
         int square = (int) Math.Pow(number, 2);
         Console.WriteLine("{0}^{1}", number, square);
         Console.WriteLine("Adding {0} to the collection...\n", square);
         numbers.Add(square);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:17,代碼來源:InvalidOperationException

輸出:

1^1
Adding 1 to the collection...


Unhandled Exception: System.InvalidOperationException: Collection was modified; 
enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at Example.Main()

示例7: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      var numbers = new List<int>() { 1, 2, 3, 4, 5 };
      
      int upperBound = numbers.Count - 1;
      for (int ctr = 0; ctr <= upperBound; ctr++) {
         int square = (int) Math.Pow(numbers[ctr], 2);
         Console.WriteLine("{0}^{1}", numbers[ctr], square);
         Console.WriteLine("Adding {0} to the collection...\n", square);
         numbers.Add(square);
      }
   
      Console.WriteLine("Elements now in the collection: ");
      foreach (var number in numbers)
         Console.Write("{0}    ", number);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:23,代碼來源:InvalidOperationException

輸出:

1^1
Adding 1 to the collection...

2^4
Adding 4 to the collection...

3^9
Adding 9 to the collection...

4^16
Adding 16 to the collection...

5^25
Adding 25 to the collection...

Elements now in the collection:
1    2    3    4    5    1    4    9    16    25

示例8: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      var numbers = new List<int>() { 1, 2, 3, 4, 5 };
      var temp = new List<int>();
      
      // Square each number and store it in a temporary collection.
      foreach (var number in numbers) {
         int square = (int) Math.Pow(number, 2);
         temp.Add(square);
      }
    
      // Combine the numbers into a single array.
      int[] combined = new int[numbers.Count + temp.Count];
      Array.Copy(numbers.ToArray(), 0, combined, 0, numbers.Count);
      Array.Copy(temp.ToArray(), 0, combined, numbers.Count, temp.Count);
      
      // Iterate the array.
      foreach (var value in combined)
         Console.Write("{0}    ", value);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:27,代碼來源:InvalidOperationException

輸出:

1    2    3    4    5    1    4    9    16    25

示例9: Person

//引入命名空間
using System;
using System.Collections.Generic;

public class Person
{
   public Person(String fName, String lName)
   {
      FirstName = fName;
      LastName = lName;
   }
   
   public String FirstName { get; set; }
   public String LastName { get; set; }
}

public class Example
{
   public static void Main()
   {
      var people = new List<Person>();
      
      people.Add(new Person("John", "Doe"));
      people.Add(new Person("Jane", "Doe"));
      people.Sort();
      foreach (var person in people)
         Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:29,代碼來源:InvalidOperationException

輸出:

Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---> 
System.ArgumentException: At least one object must implement IComparable.
at System.Collections.Comparer.Compare(Object a, Object b)
at System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
at System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
--- End of inner exception stack trace ---
at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
at Example.Main()

示例10: Person

//引入命名空間
using System;
using System.Collections.Generic;

public class Person : IComparable<Person>
{
   public Person(String fName, String lName)
   {
      FirstName = fName;
      LastName = lName;
   }
   
   public String FirstName { get; set; }
   public String LastName { get; set; }

   public int CompareTo(Person other)
   {
      return String.Format("{0} {1}", LastName, FirstName).
             CompareTo(String.Format("{0} {1}", LastName, FirstName));    
   }       
}

public class Example
{
   public static void Main()
   {
      var people = new List<Person>();
      
      people.Add(new Person("John", "Doe"));
      people.Add(new Person("Jane", "Doe"));
      people.Sort();
      foreach (var person in people)
         Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:35,代碼來源:InvalidOperationException

輸出:

Jane Doe
John Doe

示例11: Person

//引入命名空間
using System;
using System.Collections.Generic;

public class Person
{
   public Person(String fName, String lName)
   {
      FirstName = fName;
      LastName = lName;
   }
   
   public String FirstName { get; set; }
   public String LastName { get; set; }
}

public class PersonComparer : IComparer<Person>
{
   public int Compare(Person x, Person y) 
   {
      return String.Format("{0} {1}", x.LastName, x.FirstName).
             CompareTo(String.Format("{0} {1}", y.LastName, y.FirstName));    
   }       
}

public class Example
{
   public static void Main()
   {
      var people = new List<Person>();
      
      people.Add(new Person("John", "Doe"));
      people.Add(new Person("Jane", "Doe"));
      people.Sort(new PersonComparer());
      foreach (var person in people)
         Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:38,代碼來源:InvalidOperationException

輸出:

Jane Doe
John Doe

示例12: Person

//引入命名空間
using System;
using System.Collections.Generic;

public class Person
{
   public Person(String fName, String lName)
   {
      FirstName = fName;
      LastName = lName;
   }
   
   public String FirstName { get; set; }
   public String LastName { get; set; }
}

public class Example
{
   public static void Main()
   {
      var people = new List<Person>();
      
      people.Add(new Person("John", "Doe"));
      people.Add(new Person("Jane", "Doe"));
      people.Sort(PersonComparison);
      foreach (var person in people)
         Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
   }

   public static int PersonComparison(Person x, Person y)
   {
      return String.Format("{0} {1}", x.LastName, x.FirstName).
             CompareTo(String.Format("{0} {1}", y.LastName, y.FirstName));    
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:35,代碼來源:InvalidOperationException

輸出:

Jane Doe
John Doe

示例13: Main

//引入命名空間
using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      var queryResult = new int?[] { 1, 2, null, 4 };
      var map = queryResult.Select(nullableInt => (int)nullableInt);
      
      // Display list.
      foreach (var num in map)
         Console.Write("{0} ", num);
      Console.WriteLine();   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:17,代碼來源:InvalidOperationException

輸出:

1 2
Unhandled Exception: System.InvalidOperationException: Nullable object must have a value.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at Example.
b__0(Nullable`1 nullableInt) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at Example.Main()

示例14: Main

//引入命名空間
using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      var queryResult = new int?[] { 1, 2, null, 4 };
      var numbers = queryResult.Select(nullableInt => (int)nullableInt.GetValueOrDefault());
      
      // Display list using Nullable<int>.HasValue.
      foreach (var number in numbers)
         Console.Write("{0} ", number);
      Console.WriteLine();   
      
      numbers = queryResult.Select(nullableInt => (int) (nullableInt.HasValue ? nullableInt : -1));
      // Display list using Nullable<int>.GetValueOrDefault.
      foreach (var number in numbers)
         Console.Write("{0} ", number);
      Console.WriteLine();   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:23,代碼來源:InvalidOperationException

輸出:

1 2 0 4
1 2 -1 4

示例15: Main

//引入命名空間
using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      int[] data = { 1, 2, 3, 4 };
      var average = data.Where(num => num > 4).Average();
      Console.Write("The average of numbers greater than 4 is {0}",
                    average);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:InvalidOperationException

輸出:

Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.Average(IEnumerable`1 source)
at Example.Main()


注:本文中的System.InvalidOperationException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。