본문 바로가기
CSharp/CSharp 문법

메소드 ( Method)

by MonoSoft 2024. 8. 16.
728x90
반응형

메소드 ( Method)

 

 

 

 

using System.Net.Http.Headers;
using System.Security.Cryptography;
 
namespace _06_Method
{
    internal class Program
    {
        static int Fibonacci(int n)
        {
            if (n < 2)
                return n;
            else
                return Fibonacci(n - 1) + Fibonacci(n - 2);
        }
        static void PrintProfile(string name, string phone)
        {
            if (name == "")
            {
                Console.WriteLine("이름을 입력해주세요.");
                return;
            }
 
            Console.WriteLine($"Name:{name}, phone:{phone}");
        }
        public static void Swap(int a, int b)
        {
            int temp = b;
            b = a;
            a = temp;
 
            //Console.WriteLine($"a:{a},b:{b}");
        }
        public static void Swapref(ref int a,ref int b)
        {
            int temp = b;
            b = a;
            a = temp;
 
            //Console.WriteLine($"a:{a},b:{b}");
        }
        class Product
        {
            private int price = 100;
 
            public ref int GetPrice()
            {
                return ref price;
            }
 
            public void PrintPrice()
            {
                Console.WriteLine($"Price : {price}");
            }
        }
        static void Divide(int a , int b, out int quotient, out int remainder)
        {
            quotient = a / b;
            remainder = b % a;
        }
        static int Plus(int a, int b)
        {
            Console.WriteLine("Calling int Plus(int,int)...");
            return a + b;
        }
        static int Plus(int a, int b, int c)
        {
            Console.WriteLine("Calling int Plus(int,int,int)...");
            return (a + b + c);        
        }
        static double Plus(double a, double b)
        {
            Console.WriteLine("Calling double Plus(int, double)...");
            return a + b;
        }
        static int Sum(params int[] args)
        {
            Console.WriteLine("Summing...");
            int sum = 0;
            for(int i = 0; i < args.Length; i++)
            {
                if (i > 0)
                    Console.WriteLine(", ");
 
                Console.Write(args[i]);
                sum += args[i];
            }
            Console.WriteLine();
 
            return sum;
        }
        static void PrintPRofile(string name, string phone)
        {
            Console.WriteLine($"Name:{name}, Phone:{phone}");
        }
        static void PrintProfile_chose(string name, string phone = "")
        {
            Console.WriteLine($"Name:{name},phon:{phone}");
        }
        static string ToLowerString(string input)
        {
            var arr = input.ToCharArray();
 
            for(int i=0; i<arr.Length; i++)
            {
                arr[i] = ToLowerChar(i);
            }
 
            char ToLowerChar(int i)
            {
                if (arr[i] < 65 || arr[i] > 90) //A ~ Z의 ASCII 값 : 65 ~ 90
                    return arr[i];
                else // a ~ z의 ASCII 값 : 97 ~ 122
                    return (char)(arr[i] + 32);                
            }
 
            return new string(arr);
        }
 
       
 
        static void Main(string[] args)
        {
            /*
            Console.WriteLine($"10번째 피보나치 수 : {Fibonacci(10)}");
 
            PrintProfile("", "123-4567");
            PrintProfile("홍길동", "456-1230");
            */
 
            /*
            int x = 3;
            int y = 4;
 
            Console.WriteLine($"x:{x},y:{y}");
 
            Swap(x, y);
 
            Console.WriteLine($"x:{x},y:{y}");
 
 
 
            Swapref(ref x, ref y);
 
            Console.WriteLine($"x:{x},y:{y}");
            */
 
            /*
            Product carrot = new Product();
            ref int ref_local_price = ref carrot.GetPrice();
            int normal_local_price = carrot.GetPrice();
 
            carrot.PrintPrice();
            Console.WriteLine($"Ref Local Price : {ref_local_price}");
            Console.WriteLine($"Normal_local_price : {normal_local_price}");
 
            ref_local_price = 200;
 
            carrot.PrintPrice();
            Console.WriteLine($"Ref Local Price : {ref_local_price}");
            Console.WriteLine($"Normal_local_price : {normal_local_price}");
            */
 
            /*
            int a = 20;
            int b = 3;
            //int c;
            //int d;
            Divide(a, b, out int c, out int d);
            Console.WriteLine($"a:{a},b:{b}:, a/b:{c}, a&b:{d}");
            */
 
            /*
            Console.WriteLine(Plus(1,2));
            Console.WriteLine(Plus(1, 2, 3));
            Console.WriteLine(Plus(1.0 , 2.4));
            Console.WriteLine(Plus(1, 2.4));
            */
 
            /*
            int sum = Sum(3,4,5,6,7,8,9,10);
            Console.WriteLine($"Sum : {sum}");
            */
 
            /*
            PrintProfile(name: "박찬호", phone: "010-0000-0000");
            PrintProfile(phone: "010-0000-0000", name: "박찬호");
 
            PrintProfile("박세리", "010-1111-1111");
            PrintProfile("박기기", phone : "010-1111-1111");
            */
 
            /*
            PrintProfile_chose("중근");
            PrintProfile_chose("관준","010-1111-1111");
            PrintProfile_chose(name:"봉길");
            PrintProfile_chose(name:"중근",phone:"010-2222-2222");
            */
 
            Console.WriteLine(ToLowerString("Hollo!"));
            Console.WriteLine(ToLowerString("Good Morning."));
            Console.WriteLine(ToLowerString("This is  c#"));
 
 
        }
    }
}
728x90
반응형

'CSharp > CSharp 문법' 카테고리의 다른 글

코드의 흐림제어  (0) 2024.08.01
C# 문법  (0) 2024.07.01
C# 강의 메모  (0) 2022.09.28
기초문법 정리2  (0) 2022.09.27
두 문자열 경로로 결합  (0) 2022.09.26

댓글