c# 기초구문 업데이트 완료

This commit is contained in:
2025-03-05 11:57:12 +09:00
parent 15885a2286
commit 6ebd0fb5a9
22 changed files with 740 additions and 0 deletions
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+47
View File
@@ -0,0 +1,47 @@
using System.Text;
public static class ExClass
{
// static 확장메서드를 정의. 첫번째 파라미터는
// 어떤 클래스가 사용할 지만 지정.
public static string ToChangeCase(this String str)
{
StringBuilder sb = new StringBuilder();
foreach (var ch in str)
{
if (ch >= 'A' && ch <= 'Z')
sb.Append((char)('a' + ch - 'A'));
else if (ch >= 'a' && ch <= 'z')
sb.Append((char)('A' + ch - 'a'));
else
sb.Append(ch);
}
return sb.ToString();
}
// 이 확장메서드는 파라미터 ch가 필요함
public static bool Found(this String str, char ch)
{
int position = str.IndexOf(ch);
return position >= 0;
}
}
internal class Program
{
private static void Main(string[] args)
{
string s = "This is a Test";
// s객체 즉 String객체가
// 확장메서드의 첫 파리미터임
// 실제 ToChangeCase() 메서드는
// 파라미터를 갖지 않는다.
string s2 = s.ToChangeCase();
// String 객체가 사용하는 확장메서드이며
// z 값을 파라미터로 사용
bool found = s.Found('z');
}
}