LINQ/String/IO Flashcards
LINQ
LINQ is a ‘uniform query syntax’ that allows you to retrieve data from various data sources such as arrays, collections, databases, XML files.
var result = Customers.Where(temp => temp.Location == “New York”).ToList( );
//returns a list of customers from New York location.
Advantages of LINQ
Single Syntax - To Query Multiple Data Sources
Developer uses the same LINQ syntax to retrieve information from various data sources such as collections, SQL Server database, Entity Framework DbSet’s, ADO.NET DataSet etc.
Compile-Time Checking of Query Errors
Errors in the LINQ query will be identified while compilation time / while writing the code in Visual Studio.
IntelliSence Support
The list of properties of types are shown in VS IntelliSence while writing the LINQ queries.
LINQ Extension Methods
Filtering: Where, OfType
Sorting: OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse
Grouping: GroupBy
Join: Join
Project: Select, SelectMany
Aggregation: Average, Count, Max, Min, Sum
Quantifiers: All, Any, Contains
Elements: ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
Set Operations: Distinct, Except, Intersect, Union
Partitioning: Skip, SkipWhile, Take, TakeWhile
Concatenation: Concat
Equality: SequenceEqual
Generation: DefaultEmpty, Empty, Range, Repeat
Conversion: AsEnumerable, AsQueryable, Cast, ToArray, ToDictionary, ToList
String
The System.String is a class that represents an array of Unicode characters.
String stores a set of characters as char[].
Max no. of characters in the string: 2 billion.
String is immutable. That means, it can’t be modified.
Methods of ‘String’
- string ToUpper( )
- string ToLower( )
- string Substring(int startIndex, int length)
Returns a string with a set of characters starting from the “startIndex” up to the “length” no. of characters.
The “length” parameter is optional.
- string Replace( string oldString, string newString )
Returns a string after replacing all occurrences of oldString with newString.
- string[] Split( char separator )
Returns a string[] after splitting the current string into an array of characters.
At each occurrence of separator character, a new string is formed-up.
At last, all the pieces of strings are converted as a string[].
- string Trim()
Returns the same string after removing extra spaces at beginning and ending of the current string.
It returns the same string if no spaces found at L.H.S. and R.H.S. of the current string.
- string ToCharArray()
Returns the same string as an array of characters (char[]).
- static string Join(string separator, IEnumerable<T> values)</T>
Returns the a string with joined values with separator in between each value.
- bool Equals( string otherString )
Returns a Boolean value that indicates whether the current string and the specified otherString are equal or not (each character will be compared)
- bool StartsWith( string otherString)
Returns a Boolean value that indicates whether the current string beings with the specified otherString.
- bool EndsWith( string otherString )
Returns a Boolean value that indicates whether the current string ends with the specified otherString.
- bool Contains( string otherString)
Returns a Boolean value that indicates whether the current string contains the specified otherString anywhere.
- int IndexOf( string otherString, int startIndex )
Returns index of the first character of the specified otherString, where the otherString exists in the current string. Searching process starts from the specified startIndex. The startIndex is optional.
In case if the specified otherString doesn’t exist in the current string, the method returns -1.
- int LastIndexOf( string otherString, int startIndex)
It is same as IndexOf(); but searching process takes place from Right-To-Left direction, staring from the startIndex.
- static bool IsNullOrEmpty( string value)
It determines whether the given string value is null or empty (“”).
- static bool IsNullOrWhiteSpace( string value)
It determines whether the given string value is null or white space (“ “).
- static string Format( string format, object arg0, object arg1, …)
Returns the string after substituting the specified argument values at specified placeholders.
A place holder is represented as {indexOfArgument}.
Eg: string.Format(“{0} Oriented {1}”, “Object”, “Programming”) //Object Oriented Programming
18.string Insert(int startIndex, string value)
Returns a new string object which inserts the new string value at the specified index in the existing string object.
19.string Remove( int startIndex, int count)
It returns a new string object after removing specified count of characters at the specified start index, from the current string object.
Constructors of ‘String’
- String( char[] )
It initializes the string with specified char[].
StringBuilder
The System.Text.StringBuilder is a class that represents an appendable string.
The string value stored in StringBuilder can be modified, without recreating StringBuilder object.
The ‘Capacity’ property of String Builder represents the number of characters that can be stored in the string builder, as per current memory allocation.
When the programmer tries to store much more number of characters than the Capacity, String Builder automatically increases the ‘Capacity’ property to its double.
The default value of ‘Capacity’ is 16; the programmer can assign its value via constructor or set accessor of the ‘Capacity’ property.
Constructors of ‘StringBuilder’
- StringBuilder( )
It initializes the StringBuilder type of object with the default capacity (16).
- StringBuilder( int capacity )
It initializes the StringBuilder type of object with the specified capacity.
- StringBuilder( string value )
It initializes the StringBuilder type of object with the specified value.
- StringBuilder( string value, int capacity )
It initializes the StringBuilder type of object with the specified value and capacity.
Properties of ‘StringBuilder’
- int Length { get; set; }
This property gets / sets the length (number of characters) in the string builder.
- char [int index] { get; set; }
This indexer gets / sets the single character at the specified character index.
- int Capacity { get; set; }
This property returns the number of characters that can be stored in the current string builder (currently memory allocated). Default is 16.
- int MaxCapacity { get; }
This property represents maximum number of characters up to which, the Capacity can be extended.
Default is int.MaxValue.
Methods of ‘StringBuilder’
- string Append(string value)
Adds the given string value at the end of current value of string builder.
- string Insert(int startIndex, string value)
Returns a new string object which inserts the new string value at the specified index in the existing string object.
- string Remove( int startIndex, int count)
It returns a new string object after removing specified count of characters at the specified start index, from the current string object.
- string Replace( string oldString, string newString )
Returns a string after replacing all occurrences of oldString with newString.
- string ToString()
Returns the current value of string builder as a string object.
When to use ‘String’
When you would like to make less number of changes to the string.
When you perform limited number of concatenation operations.
When you want to perform extensive search operations using methods such as IndexOf, Contains, StartsWith etc.
When to use ‘String Builder’
When you would like to unknown number of / extensive number of changes to a string (Eg: making changes / concatenations to string through a loop).
When you perform less number of search operations on strings.
DateTime
The System.DateTime is a structure that represents date and time value.
Default format: yyyy-MM-dd hh:mm:ss.fff tt
‘DateTime’ Constructors
- DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
It creates a new instance (structure instance) with the specified date and time values.
“hour” should specified 24-hour format (0 to 23).
DateTime Properties
1. int Day { get; }
It returns the day (1 to 31) of current date & time value.
- int Month { get; }
It returns the month (1 to 12) of current date & time value.
- int Year { get; }
It returns the year (1 to 9999) of current date & time value.
- int Hour { get; }
It returns the hour (0 to 23) of current date & time value.
- int Minute { get; }
It returns the minute (0 to 59) of current date & time value.
- int Second { get; }
It returns the second (0 to 59) of current date & time value.
- int Millisecond { get; }
It returns the millisecond (0 to 999) of current date & time value.
- int DayOfYear { get; }
It returns the day of year (1 to 366) based on current date & time value.
- DayOfWeek DayOfWeek { get; }
It returns the day of week - Sunday to Saturday (0 to 6) based on current date & time value.
- static DateTime Now { get; }
It returns an instance of DateTime structure that represents the current system date & time.
DateTime Methods
- string ToString()
It returns the date & time value in the default date format, based on current Windows settings.
- string ToString(string format)
It returns the date & time value in the specified format.
- string ToShortDateString()
It returns the date value in the default short date format, based on current Windows settings.
Eg: MM/dd/yyyy | 12/31/2030
- string ToLongDateString()
It returns the date value in the default long date format, based on current Windows settings.
Eg: dd MMMM yyyy | 31 December 2030
- string ToShortTimeString()
It returns the date value in the default short time format, based on current Windows settings.
Eg: hh:mm tt | 11:59 pm
- string ToLongTimeString()
It returns the date value in the default long time format, based on current Windows settings.
Eg: hh:mm:ss tt | 11:59:59 pm
- static int DaysInMonth(int year, int month)
It returns number of days in the specified month in the specified year.
- static DateTime Parse(string value)
It creates and returns a new instance of DateTime structure, based on the given date string.
It uses the system default date format or “yyyy-MM-dd hh:mm:ss.fff tt” format.
Eg: “2030-12-31 11:59:59.999 pm”
- static DateTime ParseExact(string value, string format, IFormatProvider provider, DateTimeStyle style)
It creates and returns a new instance of DateTime structure, by converting (parsing) the given string value into DateTime instance, which is in the specified format.
- int CompareTo(DateTime value)
It returns:
-1: This instance value is earlier than value.
0: This instance value is equal to value.
1: This instance value is later than value.
- TimeSpan Subtract(DateTime value)
It returns an instance of TimeSpan structure, that represents date difference between the current instance and given date value.
- DateTime AddDays(double value)
It creates and returns a new instance of DateTime structure, after adding specified days (+ve or -ve) to the current date & time value.
- DateTime AddMonths(double value)
It creates and returns a new instance of DateTime structure, after adding specified months (+ve or -ve) to the current date & time value.
- DateTime AddYears(double value)
It creates and returns a new instance of DateTime structure, after adding specified years (+ve or -ve) to the current date & time value.
- DateTime AddHours(double value)
It creates and returns a new instance of DateTime structure, after adding specified hours (+ve or -ve) to the current date & time value.
- DateTime AddMinutes(double value)
It creates and returns a new instance of DateTime structure, after adding specified minutes (+ve or -ve) to the current date & time value.
- DateTime AddSeconds(double value)
It creates and returns a new instance of DateTime structure, after adding specified seconds (+ve or -ve) to the current date & time value.
- DateTime AddMilliseconds(double value)
It creates and returns a new instance of DateTime structure, after adding specified milliseconds to the current date & time value.