当前位置: 首页>>代码示例>>C#>>正文


C# ArgumentOutOfRangeException类代码示例

本文整理汇总了C#中System.ArgumentOutOfRangeException的典型用法代码示例。如果您正苦于以下问题:C# ArgumentOutOfRangeException类的具体用法?C# ArgumentOutOfRangeException怎么用?C# ArgumentOutOfRangeException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ArgumentOutOfRangeException类属于System命名空间,在下文中一共展示了ArgumentOutOfRangeException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

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

public class Example
{
   public static void Main()
   {
      var list = new List<String>();
      Console.WriteLine("Number of items: {0}", list.Count);
      try {
         Console.WriteLine("The first item: '{0}'", list[0]);
      }
      catch (ArgumentOutOfRangeException e) {
         Console.WriteLine(e.Message);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:ArgumentOutOfRangeException

输出:

Number of items: 0
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

示例2: Main

//引入命名空间
using System;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Guest guest1 = new Guest("Ben", "Miller", 17);
            Console.WriteLine(guest1.GuestInfo());
        }
        catch (ArgumentOutOfRangeException outOfRange)
        {

            Console.WriteLine("Error: {0}", outOfRange.Message);
        }
    }
}

class Guest
{
    private string FirstName;
    private string LastName;
    private int Age;

    public Guest(string fName, string lName, int age)
    {
        FirstName = fName;
        LastName = lName;
        if (age < 21)
            throw new ArgumentOutOfRangeException("age","All guests must be 21-years-old or older.");
        else
            Age = age;
    }

    public string GuestInfo()
    {
        string gInfo = FirstName + " " + LastName + ", " + Age.ToString();
        return(gInfo);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:42,代码来源:ArgumentOutOfRangeException

示例3: Main

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

public class Example
{
   public static void Main()
   {
      var numbers = new List<int>();
      numbers.AddRange( new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 } );
      
      var squares = new List<int>();
      for (int ctr = 0; ctr < numbers.Count; ctr++)
         squares[ctr] = (int) Math.Pow(numbers[ctr], 2); 
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:16,代码来源:ArgumentOutOfRangeException

输出:

Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at Example.Main()

示例4:

var squares = new List<int>();
for (int ctr = 0; ctr < numbers.Count; ctr++)
   squares.Add((int) Math.Pow(numbers[ctr], 2));
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:ArgumentOutOfRangeException

示例5: Main

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

public class Example
{
   public static void Main()
   {
      var list = new List<String>(); 
      list.AddRange( new String[] { "A", "B", "C" } );
      // Get the index of the element whose value is "Z".
      int index = list.FindIndex((new StringSearcher("Z")).FindEquals);
      try {
         Console.WriteLine("Index {0} contains '{1}'", index, list[index]); 
      }
      catch (ArgumentOutOfRangeException e) {
         Console.WriteLine(e.Message);
      }
   }
}

internal class StringSearcher
{
   String value;
   
   public StringSearcher(String value)
   {
      this.value = value;
   }
   
   public bool FindEquals(String s) 
   {
      return s.Equals(value, StringComparison.InvariantCulture); 
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:35,代码来源:ArgumentOutOfRangeException

输出:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

示例6: StringSearcher

// Get the index of the element whose value is "Z".
int index = list.FindIndex((new StringSearcher("Z")).FindEquals);
if (index >= 0)
   Console.WriteLine("'Z' is found at index {0}", list[index]);
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:ArgumentOutOfRangeException

示例7: Main

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

public class Example
{
   public static void Main()
   {
      var list = new List<String>(); 
      list.AddRange( new String[] { "A", "B", "C" } );
      try {
         // Display the elements in the list by index.
         for (int ctr = 0; ctr <= list.Count; ctr++) 
            Console.WriteLine("Index {0}: {1}", ctr, list[ctr]);
      } 
      catch (ArgumentOutOfRangeException e) {
         Console.WriteLine(e.Message);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:ArgumentOutOfRangeException

输出:

Index 0: A
Index 1: B
Index 2: C
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

示例8:

// Display the elements in the list by index.
for (int ctr = 0; ctr < list.Count; ctr++) 
   Console.WriteLine("Index {0}: {1}", ctr, list[ctr]);
开发者ID:.NET开发者,项目名称:System,代码行数:3,代码来源:ArgumentOutOfRangeException

示例9: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
       String[] words = { "the", "today", "tomorrow", " ", "" };
       foreach (var word in words)
          Console.WriteLine("First character of '{0}': '{1}'", 
                            word, GetFirstCharacter(word));
   }
   
   private static char GetFirstCharacter(String s)
   {
      return s[0];
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:ArgumentOutOfRangeException

输出:

First character of //the//: //t//
First character of //today//: //t//
First character of //tomorrow//: //t//
First character of // //: // //

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Example.Main()

示例10: GetFirstCharacter

static char GetFirstCharacter(String s)
{
   if (String.IsNullOrEmpty(s)) 
      return '\u0000';
   else   
      return s[0];
}
开发者ID:.NET开发者,项目名称:System,代码行数:7,代码来源:ArgumentOutOfRangeException

示例11: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      String[] phrases = { "ocean blue", "concerned citizen", 
                           "runOnPhrase" };
      foreach (var phrase in phrases)
         Console.WriteLine("Second word is {0}", GetSecondWord(phrase));
   }
  
   static String GetSecondWord(String s)
   {
      int pos = s.IndexOf(" ");
      return s.Substring(pos).Trim();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:19,代码来源:ArgumentOutOfRangeException

输出:

Second word is blue
Second word is citizen

Unhandled Exception: System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.
Parameter name: startIndex
at System.String.Substring(Int32 startIndex, Int32 length)
at Example.GetSecondWord(String s)
at Example.Main()

示例12: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      String[] phrases = { "ocean blue", "concerned citizen", 
                           "runOnPhrase" };
      foreach (var phrase in phrases) {
         String word = GetSecondWord(phrase);
         if (! String.IsNullOrEmpty(word))
            Console.WriteLine("Second word is {0}", word);
      }   
   }
  
   static String GetSecondWord(String s)
   {
      int pos = s.IndexOf(" ");
      if (pos >= 0)
         return s.Substring(pos).Trim();
      else
         return String.Empty;   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:ArgumentOutOfRangeException

输出:

Second word is blue
Second word is citizen

示例13: Main

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

public class Example
{
   public static void Main()
   {
      String sentence = "This is a simple, short sentence.";
      Console.WriteLine("Words in '{0}':", sentence);
      foreach (var word in FindWords(sentence))
         Console.WriteLine("   '{0}'", word);
   }
   
   static String[] FindWords(String s)
   {
      int start = 0, end = 0;
      Char[] delimiters = { ' ', '.', ',', ';', ':', '(', ')' };
      var words = new List<String>();

      while (end >= 0) {
         end = s.IndexOfAny(delimiters, start);
         if (end >= 0) {
            if (end - start > 0)
               words.Add(s.Substring(start, end - start)); 

            start = end++;
         }
         else {
            if (start < s.Length - 1)
               words.Add(s.Substring(start));
         }
      }    
      return words.ToArray();                         
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:36,代码来源:ArgumentOutOfRangeException

输出:

Words in 'This is a simple, short sentence.':
'This'
'is'
'a'
'simple'
'short'
'sentence'

示例14: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      int dimension1 = 10;
      int dimension2 = -1;
      try {
         Array arr = Array.CreateInstance(typeof(String), 
                                          dimension1, dimension2);
      }
      catch (ArgumentOutOfRangeException e) {
         if (e.ActualValue != null)
            Console.WriteLine("{0} is an invalid value for {1}: ", e.ActualValue, e.ParamName);
         Console.WriteLine(e.Message);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:20,代码来源:ArgumentOutOfRangeException

输出:

Non-negative number required.
Parameter name: length2

示例15:

int dimension1 = 10;
int dimension2 = 10;
Array arr = Array.CreateInstance(typeof(String), 
                                 dimension1, dimension2);
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:ArgumentOutOfRangeException


注:本文中的System.ArgumentOutOfRangeException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。