블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2729)
Unity3D (813)
Programming (474)
Python (8)
TinyXML (5)
STL (13)
D3D (3)
MFC (1)
C/C++ (54)
C++/CLI (45)
C# (250)
WinForm (6)
WPF (5)
Math (10)
A.I. (1)
Win32API (11)
Algorithm (3)
Design Pattern (7)
UML (1)
MaxScript (1)
FMOD (4)
FX Studio (1)
Lua (2)
Terrain (1)
Shader (3)
boost (2)
Xml (2)
JSON (4)
Etc (11)
Monad (1)
Html5 (4)
Qt (1)
Houdini (0)
Regex (10)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (227)
협업 (57)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (51)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
03-19 12:45

How do I get a Dictionary key by value in C#?

Dictionary<string, string> types = new Dictionary<string, string>()
{
    {"1", "one"},
    {"2", "two"},
    {"3", "three"}
};

 

 

[Answer]

Values do not necessarily have to be unique, so you have to do a lookup. You can do something like this:

var myKey = types.FirstOrDefault(x => x.Value == "one").Key;

If values are unique and are inserted less frequently than read, then create an inverse dictionary where values are keys and keys are values.

 

 

[출처] https://stackoverflow.com/questions/2444033/get-dictionary-key-by-value

 

Get dictionary key by value

How do I get a Dictionary key by value in C#? Dictionary<string, string> types = new Dictionary<string, string>() { {"1", "one"}, {"2", "two&q...

stackoverflow.com

 

[참조] https://miuna3.tistory.com/81

 

C# 사전 Dictionary의 Value 값으로 Key 찾기

Dictionary Dic = new Dictionary() { {"1", "Mon"}, {"2", "Tue"}, {"3", "Thu"} };사전이 위와같이 있다고 가정하자. 현재 Dic 의 키 "1" 은 값 "Mon"을 가지고있다. "Mon"으로 키 "1"을 찾고자 할 때에는 아래와 같이 사용하

miuna3.tistory.com

 

반응형
Posted by blueasa
, |

[에러 로그]

[Exception] ArgumentOutOfRangeException: Year, Month, and Day parameters describe 
            an un-representable DateTime.

----

 

개발을 하면서 연도와 상관없이 날짜(월/일/시/분/초) 비교를 위해 임의로 현재 날짜를 9999년으로 변환하게 해놨는데..

하필 오늘이 2024년 2월 29일 윤일(閏日)이다.

 

2024년 2월 29일에서 연도만 9999년으로 바꾸면서 new DateTime()을 하려니 위 에러가 발생했다.

잘 돌아가나 했던 로직이 윤일(閏日)에 에러가 남..(잘못만든거..)

 

해당 월(Month)에 해당 일(Day)이 있는지 체크하는 함수가 있으니 아래와 같이 체크해보고 처리하던가,

if (DateTime.DaysInMonth(2024,2) == 29)
    dt = new DateTime(2024, 2, 29);

 

아니면 다른 방식으로 처리를 해야 될 것 같다.

 

나의 경우는 연도를 같게 만들고 나머지를 비교하기 위해 무조건 9999년(임의 연도)으로 만들었는데..

에러가 나서 반대로 비교할 DateTime이 9999년이면 현재 연도(DateTime.Now.Year)에 맞추게 변경했다.

 

각자 원하는 방식으로 예외처리를 하면 될 것 같다.

 

[참조] https://stackoverflow.com/questions/11246563/year-month-and-day-parameters-describe-an-un-representable-datetime-exception

 

Year, Month, and Day parameters describe an un-representable DateTime Exception

I'm adding an object to a list within an ASP.NET MVC 3 application using the following code but one of the properties of the object is giving me difficulties. ls.Add(new UserRoleModel { UserRoleId...

stackoverflow.com

 

 

 

반응형
Posted by blueasa
, |

[링크] C# 특정 문자열 삭제, 특정 문자열 교체 Regex.Replace

 

C# 특정 문자열 삭제, 특정 문자열 교체 Regex.Replace

특정 문자열과, 삭제할 단어 혹은 문자가 주어졌을 때, 삭제하거나 교체하는 방법을 알아보도록 하자. 예시 Hello my world! 가 주어졌을 때, o와 y를 제외하고 출력하기 -> Hell m wrld! 기본적인 방법 : R

mentum.tistory.com

 

반응형
Posted by blueasa
, |

String Format for Int [C#]

Integer numbers can be formatted in .NET in many ways. You can use static method String.Format or instance method int.ToString. Following examples shows how to align numbers (with spaces or zeroes), how to format negative numbers or how to do custom formatting like phone numbers.

Add zeroes before number

To add zeroes before a number, use colon separator „:“and write as many zeroes as you want.

[C#]

String.Format("{0:00000}", 15);          // "00015"
String.Format("{0:00000}", -15);         // "-00015"

Align number to the right or left

To align number to the right, use comma „,“ followed by a number of characters. This alignment option must be before the colon separator.

[C#]

String.Format("{0,5}", 15);              // "   15"
String.Format("{0,-5}", 15);             // "15   "
String.Format("{0,5:000}", 15);          // "  015"
String.Format("{0,-5:000}", 15);         // "015  "

Different formatting for negative numbers and zero

You can have special format for negative numbers and zero. Usesemicolon separator „;“ to separate formatting to two or three sections. The second section is format for negative numbers, the third section is for zero.

[C#]

String.Format("{0:#;minus #}", 15);      // "15"
String.Format("{0:#;minus #}", -15);     // "minus 15"
String.Format("{0:#;minus #;zero}", 0);  // "zero"

Custom number formatting (e.g. phone number)

Numbers can be formatted also to any custom format, e.g. like phone numbers or serial numbers.

[C#]

String.Format("{0:+### ### ### ###}", 447900123456); // "+447 900 123 456"
String.Format("{0:##-####-####}", 8958712551);       // "89-5871-2551"

출처 http://www.csharp-examples.net/examples/
반응형
Posted by blueasa
, |

[링크] https://amiandappi.tistory.com/16

 

[C#][.NET framework] Directory.GetFiles() 로 여러 확장자 필터링 하기

지난번 포스팅에서 폴더 내 파일 목록을 가져오는 방법에 대해 공유 했다면 이번엔 복수개의 확장자로 필터링 하는 방법에 대해 포스팅 하려고 한다. var files = Directory.EnumerateFiles("C:\\path", "*.*", S

amiandappi.tistory.com

 

반응형
Posted by blueasa
, |

[링크] https://mirwebma.tistory.com/136

 

[C#/.Net][String To DateTime] 문자형을 Datetime형으로 변경하기

 Mir의 운영환경 본체 Intel Stick PC (STK2M3W64CC) O S Windows10 Home Application Micorsoft Visual Studio 2010 (10.0.30319.1) .Net Framework Ver 4.0 문자형을 DateTime형으로 변경하기 날짜와 시간의..

mirwebma.tistory.com

 

반응형
Posted by blueasa
, |

[링크]

https://afsdzvcx123.tistory.com/entry/C-%EB%AC%B8%EB%B2%95-C-%EB%AC%B8%EC%9E%90%EC%97%B4%EC%97%90%EC%84%9C-%EA%B3%B5%EB%B0%B1%EB%9D%84%EC%96%B4%EC%93%B0%EA%B8%B0-%EC%B2%B4%ED%81%AC%ED%99%95%EC%9D%B8-%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95

 

[C# 문법] C# 문자열에서 공백(띄어쓰기) 체크(확인) 하는 방법

안녕하세요. 오늘은 C# 문법으로 문자열에서 공백, 띄어쓰기가 중간에 포함되어 있는지 확인하는 방법에 대해서 알려드리고자 합니다. 예제코드를 통하여 바로 알아볼게요!^^ 예제 코드 1 2 3 4 5 6

afsdzvcx123.tistory.com

 

반응형
Posted by blueasa
, |

[링크] https://developer-talk.tistory.com/326

 

[C#]List 특정 값 존재하는지 체크하는 방법

이번 포스팅은 C#의 List에서 특정 값이 존재하는지 체크하는 방법을 소개합니다. 목차 Contains() 함수 Exists() 함수 FindIndex() 함수 데이터 형식이 객체인 List Contains() 함수 Contains() 함수 구문은 다..

developer-talk.tistory.com

 

반응형
Posted by blueasa
, |

[링크] https://developstudy.tistory.com/74

 

C# 자주 쓰는 Collection 집계 함수

자주 쓰는 컬렉션 집계함수들을 모아서 간단하게 사용법을 정리해보았다. 아래에서 사용한 함수는 아래와 같다. 0. Foreach 1. FindAll(찾기) 2. Except(차집합) 3. ToDictionary(사전화) 4. Select(골라내기) 5...

developstudy.tistory.com

 

반응형
Posted by blueasa
, |

참고

[ http://blog.naver.com/PostView.nhn?blogId=teshe&logNo=140055084055&widgetTypeCall=true ]

 

화면에 \3000 이렇게 표시하고 싶었는데 

""\\"" 를 사용하면 역슬래시만 표시 된다. 

 

그래서 \를 표시하기 위해 찾아봤는데 아래의 NumberFormatInfo 클래스를 사용하면 된다. 

 

using System.Globalization;

 

double price = 3000;

NumberFormatInfo numberFormat = new CultureInfo("ko-KR", false).NumberFormat;

Console.WriteLine(price.ToString("c", numberFormat);

 

c 대신에 n을 넣으면 3자리씩 끊어서 출력한다.

ko-KR 통화표시 부분

 

ko-KR , en-US, ja-Jp 사용하면 국가별 표시 가능.



출처: https://podo1017.tistory.com/133 [Keep Moving]

 

[c#] 한국 원화 \ 표시하기.

참고 [ http://blog.naver.com/PostView.nhn?blogId=teshe&logNo=140055084055&widgetTypeCall=true ] 화면에 \3000 이렇게 표시하고 싶었는데 ""\\"" 를 사용하면 역슬래시만 표시 된다. 그래서 \를 표시하기 위..

podo1017.tistory.com

 

반응형
Posted by blueasa
, |