1

I have this problem. I have holidays for this and and last year. You can use last year's holiday in this year. So lets say for example. I have 5 days left from 2013 and 20 days for 2014. I need something like in PHP.

$holiday = array();
$holiday['2013'] = 5;
$holiday['2014'] = 20;

So is there any better way to do this in C#?

3 Answers 3

3

Use a Dictionary<string,int>

Look here

Sign up to request clarification or add additional context in comments.

2 Comments

why string, both key and value are of integer type.
Technically the OP has Dictionary<string,int>
2

I think you are looking for Dictionary. Something like following.

Dictionary<int, int> holiday = new Dictionary<int, int>();

holiday[2013] = 5;
holiday[2014] = 20;

Its better if you use int for year instead of string type like in your PHP code.

4 Comments

'Better' is a bit subjective, we don't know exactly why he has got string literals in his example.
@MattC, better in a sense that int value could be used with DateTime constructor directly, instead of parsing. But yes you are right it depends on the OP's requirement.
yeah, not saying you're wrong. I agree with you but, as you said, in terms of the OP's question....
An int could use slightly less memory than the string, but it's not like that matters anymore. We ain't developing on 256 byte Z-80 boxes anymore.
1

A similar - but strongly-typed - collection is a Dictionary. Have a look here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.