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


C# ArgumentOutOfRangeException类代码示例

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

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

示例1: 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


示例2: 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


示例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


示例16:

if (dimension1 < 0 || dimension2 < 0) {
   Console.WriteLine("Unable to create the array.");
   Console.WriteLine("Specify non-negative values for the two dimensions.");
}   
else {
   arr = Array.CreateInstance(typeof(String), 
                              dimension1, dimension2);   
}
开发者ID:.NET开发者,项目名称:System,代码行数:8,代码来源:ArgumentOutOfRangeException


示例17: Main

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

public class Continent
{
   public String Name { get; set; }
   public int Population { get; set; }
   public Decimal Area { get; set; }   
}

public class Example
{
   static List<Continent> continents = new List<Continent>();
   static String msg; 
   
   public static void Main()
   {
      String[] names = { "Africa", "Antarctica", "Asia", 
                         "Australia", "Europe", "North America",
                         "South America" };
      // Populate the list.
      foreach (var name in names) {
         var th = new Thread(PopulateContinents);
         th.Start(name);
      }              
      Console.WriteLine(msg);
      Console.WriteLine();

      // Display the list.
      for (int ctr = 0; ctr < names.Length; ctr++) {
         var continent = continents[ctr];
         Console.WriteLine("{0}: Area: {1}, Population {2}", 
                           continent.Name, continent.Population,
                           continent.Area);
      }
   }
   
   private static void PopulateContinents(Object obj)
   {
      String name = obj.ToString();
      msg += String.Format("Adding '{0}' to the list.\n", name);
      var continent = new Continent();
      continent.Name = name;
      // Sleep to simulate retrieving remaining data.
      Thread.Sleep(50);
      continents.Add(continent);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:50,代码来源:ArgumentOutOfRangeException

输出:

Adding //Africa// to the list.
Adding //Antarctica// to the list.
Adding //Asia// to the list.
Adding //Australia// to the list.
Adding //Europe// to the list.
Adding //North America// to the list.
Adding //South America// to the list.



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()


示例18: Main

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

public class Continent
{
   public String Name { get; set; }
   public int Population { get; set; }
   public Decimal Area { get; set; }   
}

public class Example
{
   static ConcurrentBag<Continent> continents = new ConcurrentBag<Continent>();
   static CountdownEvent gate;
   static String msg = String.Empty;

   public static void Main()
   {
      String[] names = { "Africa", "Antarctica", "Asia", 
                         "Australia", "Europe", "North America",
                         "South America" };
      gate = new CountdownEvent(names.Length);
      
      // Populate the list.
      foreach (var name in names) {
         var th = new Thread(PopulateContinents);
         th.Start(name);
      }              

      // Display the list.
      gate.Wait();
      Console.WriteLine(msg);
      Console.WriteLine();

      var arr = continents.ToArray();
      for (int ctr = 0; ctr < names.Length; ctr++) {
         var continent = arr[ctr];
         Console.WriteLine("{0}: Area: {1}, Population {2}", 
                           continent.Name, continent.Population,
                           continent.Area);
      }
   }
   
   private static void PopulateContinents(Object obj)
   {
      String name = obj.ToString();
      lock(msg) { 
         msg += String.Format("Adding '{0}' to the list.\n", name);
      }
      var continent = new Continent();
      continent.Name = name;
      // Sleep to simulate retrieving remaining data.
      Thread.Sleep(25);
      continents.Add(continent);
      gate.Signal();
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:59,代码来源:ArgumentOutOfRangeException

输出:

Adding 'Africa' to the list.
Adding 'Antarctica' to the list.
Adding 'Asia' to the list.
Adding 'Australia' to the list.
Adding 'Europe' to the list.
Adding 'North America' to the list.
Adding 'South America' to the list.


Africa: Area: 0, Population 0
Antarctica: Area: 0, Population 0
Asia: Area: 0, Population 0
Australia: Area: 0, Population 0
Europe: Area: 0, Population 0
North America: Area: 0, Population 0
South America: Area: 0, Population 0



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