IReadOnlyList<T> Low version of IList<T> which provides only readonly view of list. public interface IReadOnlyList<out T> : IEnumerable<T>, IEnumerable { int Count { get; } T this[int index] { get; } }
C# – IList
IList<T> and IList IList<T> Standard interface for collection indexable by position. Functionality inherited from ICollection<T> and IEnumerable<T>. Provides the ability to read or write an element by position. public interface IList : ICollection, IEnumerable, IEnumerable { T this [int index] { get; set; } int IndexOf (T item); void Insert (int index, T item); void […]
C# – ICollection
ICollection<T> and ICollection ICollection<T> is the standard interface for countable collections of objects. It provides the ability to determine the size of a collection (Count). If the collection is writable you can also Add, Remove and Clear items from the collection. Collection can be traversed via foreach statement since it extends IEnumerable. Methods and Properties […]
C# – IEnumerable and IEnumerator
IEnumerable and IEnumerator Related Article IEnumerator Interface Elements in a collection are enumerated in a forward manner only. public interface IEnumerator { bool MoveNext(); object Current { get; } void Reset(); } MoveNext – advances the current element to the next position or returns false if there are no more elements in the collection. Current […]
C# – Order Comparison
Order Comparison Protocols are: 1. The IComparable Interfaces 2. The < and > Operators IComparable Interfaces Used for general-purpose sorting algorithms. System.String implements IComparable interfaces. string[] colors = { “Green”, “Red”, “Blue” }; Array.Sort (colors); foreach (string c in colors) Console.Write (c + ” “); // Blue Green Red // The IComparable interfaces are defined […]
C# – Equality Comparison
Equality Comparison Value Versus Referential Equality (== or !=) Value Equality – two values are equivalent in some sense. Referential Equality – two references refer to exactly the same object. By default the value types use value equality, reference types use referential equality. Value Equality // Simple value equality: int x = 5, y = […]
C# – Tuples and Guid Struct
Tuples Tuples are set of generic classes for holding a set of differently typed elements. Where each has read-only properties called Item1, Item2. Creating Tuples 1. Using Constructor var t = new Tuple (123, “Hello”); 2. Static Helper method Tuple t = Tuple.Create(123, “Hello”); 3. Using Implicit Type var t = Tuple.Create(123, “Hello”); Accessing the […]
C# – Working with Numbers
Working with Numbers Conversions Task Functions Examples Parsing base 10 numbers Parse TryParse double d = double.Parse(“3.5”); int i; bool ok = int.TryParse(“3”, out i); Parsing from base2, 8, or 16 Convert.ToIntegral int i = Convert.ToInt32(“1E”, 16); Formatting to hexadecimal ToString(“X”); string hex = 45.ToString(“X”); Lossless Numeric Conversion Implicit Cast int i = 23; double […]
C# – Formatting and Parsing
Formatting and Parsing Formatting means converting to a string. Parsing means converting from a string. ToString Gives meaningful output on all simple value types – bool, DateTime, DateTimeOffset, TimeSpan, Guid. // The simplest formatting mechanism is the ToString method. string s = true.ToString(); // Parse does the reverse: bool b = bool.Parse (s); Parse FormatException […]
C# – Dates and Times
Dates and Times Three structs can be used for dates and times: 1. DateTime 2. DateTimeOffset 3. TimeSpan TimeSpan Represents time of the day, simply the clock time with date missing. Has a resolution of 100ns, maximum value of about 10 million days and it can be positive or negative. The static Parse method does […]