Generics

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary

http://www.codeproject.com/Articles/832189/List-vs-IEnumerable-vs-IQueryable-vs-ICollection-v

Imports

using System.Collections.Generic;

IEnumerable

http://www.dotnetperls.com/ienumerable

Always accept a IEnumerable as the parameter of a function. This is more flexible about which types of parameters to accept.
public Convert(IEnumerable initialList)
{
}

List

List<Object>
Gives a list of Object

Copy a List

mProduct.mFees.ForEach(delegate(Fee mFee) { mQuote.mFees.Add(new Fee(mFee)); });

Sort a List

cRecords.Sort(delegate(Record r1, Record r2) { return (int.Parse(r1.index) - int.Parse(r2.index)); });

Find in a List

[http://blogs.dotnetnerds.com/steve/archive/2007/04/24/Implementing-List.Find-in-.NET-2.0.aspx]
Also see next bullet List.Contains.

using System;
using System.Collections.Generic;
public class Person
{
    public string LastName;
    public string FirstName;

    public static Predicate<Person> FindPredicate(Person person1)
    {
        return delegate(Person person2)
        {
            return person1.LastName == person2.LastName
                && person1.FirstName == person2.FirstName;
        };
    }

    public static Predicate<Person> FindByLastNamePredicate(string lastName)
    {
        return delegate(Person person)
        {
            return person.LastName == lastName;
        };
    }
}
Person foundPeep = myPeeps.Find(Person.FindByLastNamePredicate("Gump"));

List.Contains

http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx

Dictionary

Dictionary<KeyObject, ValueObject>
Gives a list of keys and values.
The key has to be unique.
Insert item: dictionary.Add(key, value);
Update item: dictionary[key]=newValue;
Get value: dictionary.TryGetValue("keyOfValueToGet", out value);

KeyValuePair

To be used as a list:
List<KeyValuePair<KeyObject, ValueObject»
The same as dictionary, but the key doesn't have to be unique.
Methods: .Key, .Value

Tuple

https://msdn.microsoft.com/en-us/library/system.tuple.aspx
http://stackoverflow.com/questions/13640322/what-and-when-to-use-tuple

Tuples
System.Tuple<string, int> s = new System.Tuple<string, int>
Is actually creating a class, which is expensive.

Instead use ValueTuple:
ValueTuple<string, int> v
This is a struct instead of a class, and is very cheap.

Getting multiple values from functions:
static (int a, int b, string c) x()
{
Return (1, 2, "bar");
}
(int a, int b, string c) x();

IEnumerable

The scientist approached the big cat with a notepad and a pencil in her hands. She was worried, of course. The cat was a predator, and likely to be hungry at this hour of the day. “I need to know”, she asked the cat, “do you keep a list of things to do each day?”

The cat stirred. He was a snow leopard with dark rosettes blotted onto his thick, cream colored fur. The big cat’s eyes were only half open, but he turned and focused them on her.

“I don’t keep a list, dear lady”, he said, followed by a rumbling yawn. “I keep an enumeration”.

“An enumeration?”, she asked.

“Yes, an enumeration”, he replied. “Lists are like the gold bracelet on your wrist, dear lady. Very tangible – very concrete things, lists are. Keeping a list of everything I might want to do is a burden and chore. I’d need to carry your paper, and your pencil”, he said, with his eyes focusing on her hands.

The scientist’s pencil raced across her notebook as she transcribed every word the leopard spoke. She glanced at him as he began to stare, and instinctively pulled herself a little further away.

The leopard continued. “With a list I’d have to add things, and remove things, and constantly reorder the things I want to do. Too much work”, he said, shaking his head. “Do you know what I can do with an enumeration?”, he asked.

She paused at the leopard’s question, and pushed her hair back - she wore glasses when she worked. After some thought, she asked, “Enumerate it?”

“Yes, dear lady”, said the leopard. “I can enumerate it. I enumerate the possibilities one by one, and find the perfect fit for this moment in my life. If I’m thirsty, I’ll find water. If I’m sleepy, I’ll find a place to sleep.” He tilted his head slightly to the right. “If I’m hungry, I’ll find food”, he said.

She finished writing the leopard’s last words and glanced up. Was that a tooth showing? Was he hungry now?

The cat started speaking again.

“One of the wonderful things about enumerations is they theoretically last forever. Lists have a beginning and an end – an Omega for every Alpha. With enumerations, you can keep asking for the next thing, over and over and over again. I ask for them when I’m ready to do something. If I’m tired of doing, they’ll still be there tomorrow. You might say it’s unpredictable behavior, I say I’m just being lazy. Either way, I can’t help it, it’s in my genes”. His soft voice trailed off with a tired tone.

“You intended to live forever?”, she asked. The leopard snarled. Or smiled. She couldn’t quite tell.

“No, dear lady”, he said. “I said the enumeration theoretically lasts forever. One day I’m sure my enumeration will run out of things to give me, or maybe I’ll just be too tired to ask for the next thing, so I’ll sleep forever. I don’t know how it ends. Maybe I should ask you.”

She looked at him again. She felt uneasy now, being here with a leopard. He seemed nice enough, as leopards go, and he certainly gave her interesting topics for research, but he was still a leopard. A carnivore. He was not a beast to be trifled with. She could never let her guard down again.

“I don’t know how it ends either”, she said, and closed her notepad. She tucked her pencil behind her ear, backed away from the cage, and left the leopard alone with his enumeration.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License