C#

C#이란? , C# 데이터타입

littlemk 2018. 7. 19. 11:41

C#이란?

C#은 마이크로소프트에서 개발된 객체 지향 프로그래밍 언어로서 Java 나 C++와 비슷한 면들을 많이 가지고 있다. 
C#을 이야기 하면 자연스럽게 .NET Framework을 함께 이야기 해야할 정도로 C#은 .NET Framework을 이용하여 프로그래밍하는 대표적인 언어이다. 
C#은 윈도우 프로그래밍, 웹 프로그래밍, 게임 및 모바일 프로그래밍 등 모든 영역에서 사용되는 범용 프로그래밍 언어이다.


C# 프로그램 소스코드

  • C# 프로그램은 .cs 라는 확장자 사용.
  • Visual Studio를 사용하는 경우 프로젝트를 빌드해 실행파일을 생성.
  • Visual Studio를 사용하지 않는 경우 .NET Framework에서 제공되는 C# 컴파일러  csc.exe를 이용해 실행파일 생성.


C#의 using, namespace, main은 무엇인가?





# Test용 프로젝트 생성. 

-> Visual Studio 2017 Community 에서 콘솔 앱(.NET Framework) 선택-> 파일명 입력-> 생성


※ 실행 순서
  1. 프로젝트 빌드 (Ctrl+Shift+b)
  2. 프로젝트 디버깅 (Ctrl+F5)


[Program.cs]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
       class Program
       {
              static void Main(string[] args)
              {
                     int a = 1;
                     char b = 'k';
                     string c = "test";
                     System.Console.WriteLine(a + b + c);
                    //Console.WriteLine으로도 가능. a+b+c 연산
              }
       }
}



[Program.cs]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
       class Program
       {
              static void Main(string[] args)
              {
                     /*
                     int a = 1;
                     char b = 'k';
                     string c = "test";
                     System.Console.WriteLine(a + b + c);
                     */
                     //bool
                     bool b = true;
                     //Numeric
                     short sh = -32768;
                     int i = 2147483647;
                     long l = 1234L;
                     float f = 123.45f;
                     double d1 = 123.45;
                     double d2 = 123.45d;
                     decimal d = 123.45m;
                     // Char/String
                     char c = 'a';
                     string s = "안녕하세요";
                     // DateTime 2018-07-18 17:30
                     DateTime dt = new DateTime(2018, 07, 18, 17, 30, 0);
                    // 변수 여러개 출력할 때
                     Console.WriteLine("b={0}, i={1}, l={2}" , b, i, l);
              }
       }
}






[Program.cs] [최대값, 최솟값]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
       class Program
       {
              static void Main(string[] args)
              {
                     /*
                     int a = 1;
                     char b = 'k';
                     string c = "test";
                     System.Console.WriteLine(a + b + c);
                     */
                     /*
                     //bool
                     bool b = true;
                     //Numeric
                     short sh = -32768;
                     int i = 2147483647;
                     long l = 1234L;
                     float f = 123.45f;
                     double d1 = 123.45;
                     double d2 = 123.45d;
                     decimal d = 123.45m;
                     // Char/String
                     char c = 'a';
                     string s = "안녕하세요";
                     // DateTime 2018-07-18 17:30
                     DateTime dt = new DateTime(2018, 07, 18, 17, 30, 0);
                     Console.WriteLine("b={0}, i={1}, l={2}, d1={3}, d2={4}", b, i, l, d1, d2);
                     */
                     // 최대값, 최솟값
                     //int i = 3213214;
                     //float f = 1234.34214f;
                     int i = int.MaxValue;
                     float f = float.MinValue;
                     Console.WriteLine("i={0}, f={1}", i, f);
              }
       }
}




[Program.cs][NULL]

 // NULL
 string r;
 r = null;
 Console.WriteLine(r);





[Program.cs][NullType]

 
//Nullable Type
 int? i = null;
 i = 101;
 bool? b = null;
 //int? 를 int로 할당
 Nullable<int> j = null;
 j = 10;
 int k = j.Value;
 Console.WriteLine("i={0}, b={1}, j={2}, k={3}", i, b, j, k);
/*
정수(int)나 날짜(DateTime)와 같은 Value Type은 일반적으로 NULL을 가질 수 없다. 
C# 2.0에서부터 이러한 타입들에 NULL을 가질 수 있게 하였는데, 이를 Nullable Type 이라 부른다.
C#에서 물음표(?)를 int나 DateTime 타입명 뒤에 붙이면 즉, int? 혹은 DateTime? 같이 하면 Nullable Type이 된다. 
이는 컴파일하면 .NET의 Nullable<T> 타입으로 변환된다. 
Nullable Type (예: int?) 을 일반 Value Type (예: int)으로 변경하기 위해서는 Nullable의 .Value 속성을 사용한다.
*/ 







참고 사이트 :: http://www.csharpstudy.com/CSharp/CSharp-variable.aspx