C# 연습문제 : 일반화 프로그래밍 , 예외처리 (뇌를 자극하는 C# 5.0)
2022. 9. 19. 22:22ㆍ문제/C# 연습문제
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);
}
}
}
}
'문제 > C# 연습문제' 카테고리의 다른 글
C# 연습문제 : 람다식 (뇌를 자극하는 C#5.0) (1) | 2022.09.21 |
---|---|
C# 연습문제 : 델리게이트와 이벤트 (뇌를 자극하는 C# 5.0) (0) | 2022.09.20 |
C# 연습문제 : 배열,컬렉션 (뇌를 자극하는 C# 5.0) (0) | 2022.09.18 |
C# 연습문제 : 프로퍼티 (뇌를 자극하는 C# 5.0) (0) | 2022.09.17 |
C# 연습문제 : 인터페이스 (뇌를 자극하는 C# 5.0) (0) | 2022.09.17 |