using NUnit.Framework; using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using UnityEngine; public class Chapter2 : MonoBehaviour { // const¿Í readonly ÀÇ Â÷ÀÌ // const´Â ±âº»ÀûÀ¸·Î static Æ÷ÇÔ ±×¸®°í ±âº»º¯¼ö¿¡ ¸¸ »ç¿ë°¡´É const int MyConstInt = 1000; readonly List ints = null; private decimal myDecimal1, myDecimal2; // get °£¼ÒÈ­ public decimal MyDecimal => myDecimal1 * myDecimal2; // get,set °£¼ÒÈ­ public int MyProperty { get; set; } = 2025; // ¹üÀ§ ÁöÁ¤ public decimal MyDecimal2 { get => myDecimal1; private set => myDecimal1 += value; } void Start() { Panda panda1 = new Panda("Bao Bao"); Panda panda2 = new Panda("Mei Xiang"); Panda panda3 = new Panda("Tian Tian"); Debug.Log("Panda Population: " + Panda.Population); int a = int.MaxValue; Debug.Log("Max Int: " + a); //int b= checked(a + 1); // ¿À¹öÇÃ·Î¿ì ¹ß»ý½Ã ¿¹¿Ü ¹ß»ý //int b = unchecked(a + 1); // ¿À¹öÇÃ·Î¿ì ¹ß»ý½Ã ¿¹¿Ü ¹ß»ýÇÏÁö ¾ÊÀ½ int b = a + 1; // ±âº»ÀûÀ¸·Î unchecked »óÅ Debug.Log("Overflowed Int: " + b); decimal c = decimal.MaxValue;// 10^28 -1 //Á¤È®µµ°¡ ³ôÁö¸¸ doubleº¸´Ù 10¹è ´À¸² double e = double.MaxValue;// 10^308 -1 // ºÎµ¿¼Ò¼öÁ¡ ¿¬»ê¿¡ ÀûÇÕ float d = float.PositiveInfinity;// ¾çÀÇ ¹«ÇÑ´ë Debug.Log("Max Decimal: " + c); Debug.Log("Max Double: " + e); Debug.Log("Positive Infinity Float: " + d);// Infinity bool isRaining = false; // 1¹ÙÀÌÆ®¸¦ »ç¿ëÇÑ´Ù BitArray bits = new BitArray(1);// 1ºñÆ®¸¸ »ç¿ë bits[0] = true;// ±âº»°ªÀº false Debug.Log("Is it raining? " + bits[0]); StartCoroutine(WaitAndPrint()); Debug.Log("Here's a tab:\t ÅÇ"); string filePath = @"C:\Users\Username\Documents\UnityProjects";// À̽ºÄÉÀÌÇÁ ½ÃÄö½º¸¦ ¹«½Ã @¾È¿¡´Â ¸ðµç ¹®ÀÚ°¡ ÀÏ¹Ý ¹®ÀڷΠó¸®µÊ Debug.Log("File Path: " + filePath); string multiLine = @"This is a multi-line string. It preserves line breaks and ""spaces""."; Debug.Log("Multi-line String: " + multiLine);// ÆÄÀ̽ãÀÇ ''' '''¿Í À¯»ç ±Ùµ¥ "´Â µÎ¹ø ½á¾ßÇÔ // $¿Í ÀÀ¿ëÇϸé string testTxt = $@"This is a test file path: {filePath} And here's a tab character:\tEnd of tab."; Debug.Log("Test Text: " + testTxt); // [,]°ú [][]ÀÇ Â÷ÀÌ // ´ÙÂ÷¿ø ¹è¿­ (°íÁ¤ Å©±â Á÷»ç°¢Çü) int[,] rect = new int[2, 3]; rect[0, 0] = 1; rect[1, 2] = 9; // Àç±×µå ¹è¿­ (Çึ´Ù ±æÀ̰¡ ´Ù¸¦ ¼ö ÀÖÀ½) int[][] jagged = new int[2][]; jagged[0] = new int[3]; // ±æÀÌ 3 jagged[1] = new int[1]; // ±æÀÌ 1 jagged[0][0] = 1; jagged[1][0] = 9; // static Çʵå¿Í ¹è¿­Àº ±âº»À¸·Î 0À¸·Î ÃʱâÈ­µÊ Debug.Log("Static Int: " + staticInt); Debug.Log("myArrar 5: " + myArray[4]); // string°ú °°Àº ÂüÁ¶Çü½ÄÀº null·Î ÃʱâÈ­µÊ decimal myD = default(decimal); // 0 //default¿Í µ¿ÀÏ decimal myD2; Debug.Log("Default Decimal: " + myD); //Debug.Log("Default Decimal 2: " + myD2);// Áö¿ªº¯¼ö´Â ¹Ýµå½Ã ÃʱâÈ­ ÇØ¾ßÇÔ //¸Å°èº¯¼ö·Î °ªÀ» º¹»ç ÇÏ´Â°Ô ¾Æ´Ñ ÂüÁ¶¸¦ º¹»ç ÇÏ´Â°Ô Àִµ¥ ÀÔ·ÂÀº ref, Ãâ·ÂÀº out // out¸¦ »ç¿ëÇÏ¸é ¿©·¯°³ÀÇ ¹Ýȯ°ªÀ» Èä³»³¾ ¼ö ÀÖÀ½ int refValue = 10;// ref´Â ¹Ýµå½Ã ÃʱâÈ­ ÇØ¾ßÇÔ TestRef(ref refValue); Debug.Log("Ref Value after TestRef: " + refValue); int outValue1 = 20;// out´Â ÃʱâÈ­ ÇÏÁö ¾Ê¾Æµµ µÊ TestOut(out outValue1, out refValue); Debug.Log("Out Value1 after TestOut: " + outValue1); Debug.Log("Out Value2 after TestOut: " + refValue); //°¡º¯ ¸Å°³º¯¼ö params int sum1 = MySum(1, 2, 3); Debug.Log("Sum1: " + sum1); // ±âº»°ª Çʵå MyTestBasicNumber(); // ¸í¸íµÈ Àμö MyTestBasicNumber(20, y: 20.5f, z: "¾È³çÇϼ¼¿ä"); MyTestBasicNumber(y: 15.5f, x: 20, z: "¼ø¼­º¯°æ"); // ¸Å°³º¯¼öÀÇ ¼ø¼­°¡ ´Þ¶óµµ ¸í¸íµÈ Àμö¸¦ »ç¿ëÇÏ¸é ¹®Á¦ ¾øÀ½ // ¸Å°³º¯¼ö°¡ ¸¹¾Æ Áö¸é ³ª¸§ Á÷°üÀûÀÓ string testNullValue = null; string testNonNullValue = testNullValue ?? "null °ªÀÔ´Ï´Ù.";// null º´ÇÕ ¿¬»êÀÚ Debug.Log("Test Null Value: " + testNonNullValue); Debug.Log("Test null value: " + testNullValue?.ToString()); //null Á¶°ÇºÎ ¿¬»êÀÚ //Debug.Log("Test null value: " + testNullValue.ToString()); // null Æ÷ÀÎÆ® ¿¡·¯ ¹ß»ý //ÀÀ¿ë Debug.Log("Test null value: " + (testNullValue?.ToString() ?? "null ÀÌ¿´½¿")); // () Çʼö // foreach stringµµ ¹ë¿­·Î º¼¼ö ÀÖ¾î ÀÌ·±½ÄÀ¸·Î °¡´ÉÇÏ´Ù foreach (char ch in "¾È³çÇϼ¼¿ä") { Debug.Log(ch); } // nameof º¯¼öÀÇ À̸§À» È®ÀÎ Debug.Log("myDecimal1 name : " + nameof(myDecimal1)); object obj = new Panda("Bao Bao"); // ¹æ¹ý 1: is ÆÐÅÏ if (obj is Panda panda) { Debug.Log($"´Ù¿îij½ºÆÃ ¼º°ø: {panda.Name}"); } //°¡»ó ÇÔ¼ö »ç¿ëÇϱâ MyAsset myAsset = new MyAsset(); Debug.Log(myAsset.ClassName); Debug.Log(myAsset.Liability); // base test »ý¼ºÀÚ ¼ø¼­ È®ÀÎ MyAsset myasset = new MyAsset(1); MyAsset2 myAsset2 = new MyAsset2(); Debug.Log(myAsset2.NetValue); Asset2 asset2 = myAsset2;// ¾÷½ºÄÉÀÏ ÈÄ // overrider¿Í new Â÷ÀÌ asset2.MyOverrider();// ÀçÁ¤ÀÇ µÈ ÀÛ½Ä °´Ã¼¸¦ Ãâ·Â asset2.MyNew();// ºÎ¸ð°´Ã¼¸¦ Ãâ·ÂÇÑ´Ù // sealed ·Î ÀçÁ¤ÀÇ ºÀÀÎ (¸Å¼Òµå ¹× Ŭ·¡½º¿¡ »ç¿ë°¡´É) asset2.MySealed();//ÀÚ½Ä °´Ã¼¸¦ Ãâ·Â //ÀçÁ¤ÀÇ ºÒ°¡ ¸Å¼Òµå Ãâ·Â MyWindowApi();// À©µµ¿ì API È£Ãâ } // extern Ű¿öµå¿Í DllImport ¾îÆ®¸®ºäÆ®¸¦ »ç¿ëÇÏ¿© // C#¿¡¼­ ³×ÀÌÆ¼ºê Äڵ带 È£ÃâÇÒ ¼ö ÀÖÀ½ // Windows APIÀÇ MessageBox ÇÔ¼ö È£Ãâ [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type); [ContextMenu("MY Window Open")] //[ContextMenu("À©µµ¿ì api¸¦ »ç¿ëÇÏ¿© â »ý¼º")] public static void MyWindowApi() { global::System.IntPtr owner = IntPtr.Zero; // :: ¸¦ »ç¿ëÇÏ¿© ¸í½ÃÀûÀ¸·Î ÇØ´ç System »ç¿ë ¸í½Ã MessageBox(owner, "¾È³çÇϼ¼¿ä", "Å×½ºÆ®", 0); } void TestRef(ref int value) { value += 5; } void TestOut(out int value1,out int value2) { value1 = 100; value2 = 200; } int MySum(params int[] numbers)// ¹è¿­ ÇüÅ·θ¸ Àü´Þ °¡´É { int sum = 0; foreach (int num in numbers) { sum += num; } return sum; } //int MySum2(params List numbers)// List ÇüÅ·ΠºÒ°¡ //{// C#9¿¡¼­´Â »ç¿ë ºÒ°¡ C# 13ºÎÅÍ °¡´É // int sum = 0; // foreach (int num in numbers) // { // sum += num; // } // return sum; //} // ±âº»°ª Çʵå void MyTestBasicNumber(int x= 10, float y = 5.5f, string z = "Hello") { Debug.Log($"x: {x}, y: {y}, z: {z}"); } // static Çʵå¿Í ¹è¿­Àº ±âº»À¸·Î 0À¸·Î ÃʱâÈ­µÊ static int staticInt; int[] myArray = new int[5]; /// /// 1ÃÊ µÚ¿¡ È£ÃâµÇ´Â ÄÚ·çÆ¾ ¸Þ¼­µå /// /// public System.Collections.IEnumerator WaitAndPrint() { yield return new WaitForSeconds(1f); Console.Write('\a'); // ¾ÆÁ÷ À¯´ÏƼ Äֿܼ¡¼­ ¼Ò¸®°¡ ³ªÁö ¾ÊÀ½(ºñÇÁÀ½) Debug.Log("1 second has passed."); } // Update is called once per frame void Update() { } } class Panda { public string Name; public static int Population = 0; /// /// Ŭ·¡½º È£Ãâ½Ã Áõ°¡ÇÏ´Â »ý¼ºÀÚ /// /// /// À̸§ /// public Panda(string name) { Name = name; Population = checked(Population++);// checked Ű¿öµå´Â ¿À¹öÇÃ·Î¿ì ¹ß»ý½Ã ¿¹¿Ü¸¦ ¹ß»ý½ÃÅ´ } } class Asset { public string name; /// /// °¡»ó ¸â¹ö ÇÔ¼ö (ÀÎÅÍÆäÀ̽º ó·³ °­Á¦°¡ ¾Æ´Ï´Ù ÀçÁ¤ÀÇ ¾ÈÇØµµ µÊ) /// public virtual decimal Liability => 0; /// /// ÀÏ¹Ý ÇÔ¼ö ÀçÁ¤ÀÇ ºÒ°¡ /// public decimal Libility2 => 0; int x = 1; public Asset(int x) => Debug.Log("this.x + x : " + (this.x+x)); // ±âº» »ý¼ºÀÚ »ý¼º public Asset() => Debug.Log("Asset ±âº» »ý¼ºÀÚ"); } /// /// Ãß»ó Ŭ·¹½º ±¸ÇöÀº ¾ø°í ÀçÁ¤ÀÇ Çʼö /// abstract class Asset2 { public abstract decimal NetValue { get; } // overrider, new test public virtual void MyOverrider() => Debug.Log("Asset2 Overrider"); public virtual void MyNew() => Debug.Log("Asset2 New"); // sealed test ÇÔ¼ö public virtual void MySealed() => Debug.Log("Asset2 sealed"); } class MyAsset : Asset { public override decimal Liability => base.Liability; public string ClassName { get; set; } = nameof(MyAsset); //public override decimal Liability2 =>base.Libility2; //ÄÄÆÄÀÏ ¿¡·¯ public MyAsset(int x) : base(x) => Debug.Log("MyAsset ¿¡¼­ base ½ÇÇà"); // base(x)·Î ºÎ¸ð°´Ã¼ÀÇ »ý¼ºÀÚ¸¦ ¸ÕÀú ½ÇÇàÇÏ°í ³ªÁß¿¡ ½ÇÇàµÈ´Ù // »ý¼ºÀÚ¸¦ ¸¸µé¾î¼­ ±âº» »ý¼ºÀÚ Çʼö »ý¼º public MyAsset() => Debug.Log("MyAsset ±âº» »ý¼ºÀÚ"); } class MyAsset2 : Asset2 { public override decimal NetValue => 2025; // ÀçÁ¤ÀÇ¿Í ¼û±â±â public override void MyOverrider() => Debug.Log("MyAsset2 Overrider"); public new void MyNew() => Debug.Log("MyAsset2 New"); // ÀçÁ¤ÀÇ ºÀÀÎ public sealed override void MySealed() => Debug.Log("ÀçÁ¤ÀÇ ºÒ°¡ ¸Å¼Òµå"); }