본문 바로가기

프로그래밍/C#

C# 프로그래밍 (기본데이터)

기본 데이터

기본데이터-1

C#에서 기본적으로 제공하는 자료형

 

sbyte byte short ushort int uint long ulong
float double decimal   char string   bool

 

데이터 형태 - 정수

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

namespace _001_HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("정수형 데이터 형식과 범위확인");
            Console.WriteLine("sbyte  : {0} ~ {1}", sbyte.MinValue, sbyte.MaxValue);
            Console.WriteLine("byte   : {0} ~ {1}", byte.MinValue, byte.MaxValue);
            Console.WriteLine("short  : {0} ~ {1}", short.MinValue, short.MaxValue);
            Console.WriteLine("ushort : {0} ~ {1}", ushort.MinValue, ushort.MaxValue);
            Console.WriteLine("int    : {0} ~ {1}", int.MinValue, int.MaxValue);
            Console.WriteLine("uint   : {0} ~ {1}", uint.MinValue, uint.MaxValue);
            Console.WriteLine("long   : {0} ~ {1}", long.MinValue, long.MaxValue);
            Console.WriteLine("ulong  : {0} ~ {1}", ulong.MinValue, ulong.MaxValue);
        }
    }
}

데이터 형태 - 부동 소수점, 실수

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

namespace _001_HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("실수형 데이터 형식과 범위확인");
            Console.WriteLine("float  : {0} ~ {1}", float.MinValue, float.MaxValue);
            Console.WriteLine("double   : {0} ~ {1}", double.MinValue, double.MaxValue);
            Console.WriteLine("decimal  : {0} ~ {1}", decimal.MinValue, decimal.MaxValue);  
        }
    }
}

 

데이터 형태 - 문자, 문자열, 논리

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

namespace _001_HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("문자, 문자열, 논리 데이터 형식확인");
            Console.WriteLine("char  : {0}", typeof(char));
            Console.WriteLine("string   : {0}", typeof(string));
            Console.WriteLine("bool  : {0}", typeof(bool));  
        }
    }
}

 

기본데이터-2

C#에서 기본적으로 제공하는 자료형

object enum nullable var const

데이터 형태 - object

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

namespace _001_HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //object는 객체의 데이터 형태로 어떤 데이터 형이든 모두 처리가 가능하다
            object a = 10;
            object b = 3.14;
            object c = 'A';
            object d = "ABCD";
            object e = false;

            Console.WriteLine("a: {0}", a);
            Console.WriteLine("b: {0}", b);
            Console.WriteLine("c: {0}", c);
            Console.WriteLine("d: {0}", d);
            Console.WriteLine("e: {0}", e);

        }
    }
}

데이터 형태 - enum

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

namespace _001_HelloWorld
{
    class Program
    { 
        //enum은 열거형 데이터 형태이다
        //enum 값에 특정 값을 넣을수있다
        enum Day
        {
            SUN,
            MON,
            THE = 5,
            WED,
            THU,
            FRI = 10,
            SAT,
        }
        static void Main(string[] args)
        {

            Console.WriteLine("{0},{1}", Day.SUN, (int)Day.SUN);
            Console.WriteLine("{0},{1}", Day.MON, (int)Day.MON);
            Console.WriteLine("{0},{1}", Day.THE, (int)Day.THE);
            Console.WriteLine("{0},{1}", Day.WED, (int)Day.WED);
            Console.WriteLine("{0},{1}", Day.THU, (int)Day.THU);
            Console.WriteLine("{0},{1}", Day.FRI, (int)Day.FRI);
            Console.WriteLine("{0},{1}", Day.SAT, (int)Day.SAT);
        }
    }
}

데이터 형태 - nullable

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

namespace _001_HelloWorld
{
    class Program
    { 
        static void Main(string[] args)
        {
            //nullable의 형식을 사용하면 기존 데이터형의 값에 null값을 저장할 수 있다.
            //hasvalue와 value 사용가능
            int a = 100;
            int? b = null;
            bool? c = null;

            Console.WriteLine("{0}, {1}, {2}", a, b, c);
        }
    }
}

데이터 형태 - var

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

namespace _001_HelloWorld
{
    class Program
    { 
        static void Main(string[] args)
        {
            //암시적 형식 지역변수
            //선언과 동시에 초기화
            //지역 변수로만 사용가능, for~each에서 자주사용된다.object a = 10;
            var a = 10;
            var b = 3.14;
            var c = 'A';
            var d = "ABCD";
            var e = false;
            
            Console.WriteLine("a: {0}, {1}", a, a.GetType());
            Console.WriteLine("b: {0}, {1}", b, b.GetType());
            Console.WriteLine("c: {0}, {1}", c, c.GetType());
            Console.WriteLine("d: {0}, {1}", d, d.GetType());
            Console.WriteLine("e: {0}, {1}", e, e.GetType());
        }
    }
}

데이터 형태 - const

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

namespace _001_HelloWorld
{
    class Program
    { 
        static void Main(string[] args)
        {
            //상수
            const int NUM = 100;
            //NUM = 200; 한번 정하면 바꿀 수 없음
            Console.WriteLine(NUM);           
        }
    }
}

 

기본데이터-3

형변환, readLine()

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

namespace _001_HelloWorld
{
    class Program
    { 
        static void Main(string[] args)
        {
            int num = 100;
            double dNum = (double)num;
            Console.WriteLine(num.GetType());
            Console.WriteLine(dNum.GetType());

            int a = 100;
            string strA = a.ToString();
            Console.WriteLine(a.GetType());
            Console.WriteLine(strA.GetType());

            int parseA = int.Parse(strA);
            Console.WriteLine(parseA.GetType());

            int convertA = Convert.ToInt32(strA);
            Console.WriteLine(convertA.GetType());

            Console.Write("수를 입력해주세요.");
            int aa = int.Parse(Console.ReadLine());
            Console.WriteLine("입력한 수는 {0}입니다", aa);

        }
    }
}