I wish to continue at my own risk를 클릭하거나 X를 눌러 닫으면 실행이 된다.
하지만 콘솔창에
Unity is running with Administrator privileges, which is not supported. Unity executes scripts and binary libraries in your project that may originate from third party sources and potentially be harmful to your computer. Unity may also execute scripts and binary libraries that are still under development and not yet fully tested. Running Unity with Administrator privileges may lead to catastrophic consequences, including but not limited to accidental data loss, change of global system settings or even bricking your device.
C#에서 지원하는 Parse는 여러가지가 존재한다. 가장 대표적인 예시는 int->string, string->int 정도가 있는데 int->string은 ToString(), System.Convert.ToString()이 있고 string->int는 int.Parse(), int.TryParse()가 있다. 각각의 속도는 어떨까?
using System;
public class Ex : MonoBehaviour
{
List<int> integers = new List<int>();
List<string> strs = new List<string>();
int LoopCount = 100000;
void Start()
{
for(int i = 0; i < LoopCount; i++)
{
integers.Add(i);
strs.Add(i.ToString());
}
}
void Update()
{
for(int i = 0; i < LoopCount; i++)
{
string str_1 = integers[i].ToString();
string str_2 = integers[i].System.Convert.ToString(integers[i]);
int integer_1 = int.Parse(strs[i]);
int integer_2;
int.TryParse(strs[i], out integer_2);
}
}
}
속도를 보면 int->string은 속도가 비슷하거나 뒤죽박죽 하기 때문에 입맛에 따라 쓰면 될 것 같다. string->int의 경우 int.Parse가 1000회당 0.01ms 정도 느리다. 입맛에 따라 사용해도 되긴 하지만 안전한 코드 사용을 위해 int.TryParse를 사용하는 것을 권장
"C#에서 string을 비교할 때 ==을 많이 사용하셨는데 ==을 많이 사용하신 이유가 무엇인가요?"
그냥 같다 라는 의미로 사용한거지 별 다른 이유가 없었는데 면접관분께서 좋은 이야기를 해주셨다.
"C#에서 string, int등 상수를 비교 할 때는 Equals()를 사용하는게 좋다. ==은 참조를 비교하는거고 Equals는 값을 비교하는 것이다."
Equals를 알고 있었지만 해당 내용에 대해서는 처음 듣는 이야기여서 하나 더 배웠다는 생각이 든다.
예시를 들자면
public class EX : MonoBehaviour
{
string ex1 = "Hello";
object ex2 = new string("Hello");
private void Ex()
{
if (ex1 == ex2)
{
// 의도하지 않은 참조가 발생했다고 오류 발생
// 참조가 다르기 때문에 해당 부분은 출력되지 않음
Debug.Log("True ex1 If1");
}
if (ex1.Equals(ex2))
{
// 참조가 다르지만 값이 같기 때문에 해당 부분 출력
Debug.Log("True ex1 If2");
}
}
StringBuilder sbText1 = new StringBuilder("Hello");
StringBuilder sbText2 = new StringBuilder("Hello");
private void Ex2()
{
if (sbText1 == sbText2)
{
// 참조가 다르기 때문에 해당 부분은 출력되지 않음
Debug.Log("True ex2 If1");
}
if (sbText1.Equals(sbText2))
{
// 참조가 다르지만 값이 같기 때문에 해당 부분 출력
Debug.Log("True ex2 If2");
}
}
}
해당 부분에서 이렇게 나뉜다. 의도하지 않은 조건식을 만들 수 있기 때문에 상수인 값을 비교한다면 Equals를 사용해야 할 것 같다.