using (ResourceType resource = expression) statement
{
ResourceType resource = expression;
try {
statement;
}
finally {
((IDisposable)resource).Dispose();
}
}
{
ResourceType resource = expression;
try {
statement;
}
finally {
if (resource != null) ((IDisposable)resource).Dispose();
}
}
foreach (ElementType element in collection)
E enumerator = (collection).GetEnumerator();
try {
while (enumerator.MoveNext()) {
ElementType element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
IEnumerator enumerator =
((System.Collections.IEnumerable)(collection)).GetEnumerator();
try {
while (enumerator.MoveNext()) {
ElementType element = (ElementType)enumerator.Current;
statement;
}
}
finally {
IDisposable disposable = enumerator as System.IDisposable;
if (disposable != null) disposable.Dispose();
}
using System;
using System.IO;
class RadParser
{
static void Main(String[] args)
{
StreamReader stream = new StreamReader(args[0]);
String line;
Char[] delims = { " ", "\t" };
int sum = 0;
while ((line = stream.ReadLine()) != null)
{
String[] fields = line.Split(delims);
foreach (String field in fields)
{
sum += Int32.Parse(field);
}
}
Console.WriteLine("The total of the ints is: {0,8:n0}", sum);
}
}
using System;
using System.IO;
class ClassicParser
{
static void Main(String[] args)
{
FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read);
Byte[] bytes = new Byte[4096];
int sum = 0;
int tmp = 0;
bool negative = false;
bool digits = false;
for (;;)
{
int count = fs.Read(bytes, 0, bytes.Length);
if (count <= 0)
{
if (negative)
sum -= tmp;
else
sum += tmp;
break;
}
for (int i = 0; i < count; i++)
{
switch ((char)bytes[i])
{
case " ": case "\t": case "\r": case "\n":
if (negative)
sum -= tmp;
else
sum += tmp;
negative = false;
digits = false;
tmp = 0;
break;
case "-":
if (negative)
throw new FormatException("two negatives");
if (digits)
throw new FormatException("negative after digits");
negative = true;
break;
case "0": case "1": case "2": case "3": case "4":
case "5": case "6": case "7": case "8": case "9":
digits = true;
tmp = tmp*10 + bytes[i] - (Byte)"0";
break;
default:
throw new FormatException("invalid character");
}
}
}
Console.WriteLine("The total of the ints is: {0,8:n0}", sum);
}
}
ArrayList array = new ArrayList();
...
foreach (int i in array)
{
Console.WriteLine(i);
}
ArrayList array;
int number;
IEnumerator enumerator;
IDisposable disposable;
array = new ArrayList();
...
enumerator = array.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
number = ((int)enumerator.Current);
Console.WriteLine(number);
}
}
finally
{
disposable = (enumerator as IDisposable);
if (disposable != null)
{
disposable.Dispose();
}
}
int[] array =
array = new int[]{1, 2, 3, 4, 5, 6};
foreach (int i in array)
{
Console.WriteLine(i);
}
int number;
int[] tempArray;
int[] array = new int[]{1, 2, 3, 4, 5, 6};
tempArray = array;
for (int counter = 0; (counter < tempArray.Length); counter++)
{
Console.WriteLine(tempArray[counter]);
}
이전 글 : RT-Linux 리눅스를 이용한 PC기반의 임베디드 시스템 구현
다음 글 : 프로그램의 설계시에 알아야 할 좋은 코딩 습관
최신 콘텐츠