当前位置: 首页>>代码示例>>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;未经允许,请勿转载。