본문 바로가기

C#

C# 문법 - 가변 매개 변수 Params 사용법에 대해 알아보자

반응형

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static int Add(params int[] values)
        {
            int result = 0;
            for (int i = 0; i < values.Length; i++)
            {
                result = result + values[i];
            }
            return result;
        }

        private static void PrintAll(params object[] values)
        {
            foreach (object item in values)
            {
                Console.Write(item);
            }
        }
        [DllImport("user32.dll")]
        static extern int MessageBeep(uint uType);

        static int TestMethod(uint type) => 0;

        static void Main(string[] args)
        {
            Console.WriteLine(Add(1,2,3,4,5));
            Console.WriteLine(Add(1, 2, 3, 4, 5,6,7,8,9,10));
            PrintAll(1.05, "Result", 3);
            MessageBeep(1);
        }
    } 
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static int Add(params int[] values)
        {
            int result = 0;
            for (int i = 0; i < values.Length; i++)
            {
                result = result + values[i];
            }
            return result;
        }

        private static void PrintAll(params object[] values)
        {
            foreach (object item in values)
            {
                Console.Write(item);
            }
        }
        
        static void Main(string[] args)
        {
            Console.WriteLine(Add(1,2,3,4,5));
            Console.WriteLine(Add(1, 2, 3, 4, 5,6,7,8,9,10));
            PrintAll(1.05, "Result", 3);

        }
    } 
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {

            int iNum = 100;

            //주소의 사용을 막았다 
            //c# 객체지향 언어에서 참조개념이 있기 때문에 
            //주소의 사용을 막아 놓았다
            unsafe
            {
                Console.WriteLine("{0:x}", (int)&iNum);
            }
            
        }
    }
}

https://slaner.tistory.com/55

 

21. GetWindowText

선언:C#[DllImport("user32")] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); VB.NET _ Public Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer End Functio

slaner.tistory.com

 

반응형