作者:.NET开发
项目:Syste
//引入命名空间
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);
}
}
作者:.NET开发
项目:Syste
//引入命名空间
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);
}
}
}
作者:.NET开发
项目:Syste
//引入命名空间
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);
}
}
作者:.NET开发
项目:Syste
var squares = new List<int>();
for (int ctr = 0; ctr < numbers.Count; ctr++)
squares.Add((int) Math.Pow(numbers[ctr], 2));
作者:.NET开发
项目:Syste
//引入命名空间
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);
}
}
作者:.NET开发
项目:Syste
// 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]);
作者:.NET开发
项目:Syste
//引入命名空间
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);
}
}
}
作者:.NET开发
项目:Syste
// Display the elements in the list by index.
for (int ctr = 0; ctr < list.Count; ctr++)
Console.WriteLine("Index {0}: {1}", ctr, list[ctr]);
作者:.NET开发
项目:Syste
//引入命名空间
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];
}
}
作者:.NET开发
项目:Syste
static char GetFirstCharacter(String s)
{
if (String.IsNullOrEmpty(s))
return '\u0000';
else
return s[0];
}
作者:.NET开发
项目:Syste
//引入命名空间
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();
}
}
作者:.NET开发
项目:Syste
//引入命名空间
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;
}
}
作者:.NET开发
项目:Syste
//引入命名空间
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();
}
}
作者:.NET开发
项目:Syste
//引入命名空间
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);
}
}
}
作者:.NET开发
项目:Syste
int dimension1 = 10;
int dimension2 = 10;
Array arr = Array.CreateInstance(typeof(String),
dimension1, dimension2);
作者:.NET开发
项目:Syste
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);
}
作者:.NET开发
项目:Syste
//引入命名空间
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);
}
}
作者:.NET开发
项目:Syste
//引入命名空间
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();
}
}