문제/C# 연습문제
C# 연습문제 : 일반화 프로그래밍 , 예외처리 (뇌를 자극하는 C# 5.0)
코딩너구리
2022. 9. 19. 22:22
1. 일반화 메서드 하나만을 이용해 int, float,string을 매개변수로 받아와 출력하는 프로그램을 작성하세요.
using System;
namespace BookTestC
{
public class Program
{
private static void Main(string[] args)
{
int a = 10;
float b = 5.3f;
string c = "안녕";
Print(a);
Print(b);
Print(c);
}
public static void Print<T>(T value)
{
Console.WriteLine(value);
}
}
}
2.try~catch 문을 이용해 예외처리가 될 수 있는 for문 코드를 작성하세요
using System;
namespace BookTestC
{
public class Program
{
private static void Main(string[] args)
{
int[] a = new int[] { 1, 2, 3 };
try
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(a[i]);
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine(e.Message);
}
}
}
}