LINQ/String/IO Flashcards

1
Q

LINQ

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

Advantages of LINQ

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

LINQ Extension Methods

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

String

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Methods of ‘String’

A
  1. string ToUpper( )
  2. string ToLower( )
  3. 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.

  1. string Replace( string oldString, string newString )

Returns a string after replacing all occurrences of oldString with newString.

  1. 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[].

  1. 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.

  1. string ToCharArray()

Returns the same string as an array of characters (char[]).

  1. static string Join(string separator, IEnumerable<T> values)</T>

Returns the a string with joined values with separator in between each value.

  1. 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)

  1. bool StartsWith( string otherString)

Returns a Boolean value that indicates whether the current string beings with the specified otherString.

  1. bool EndsWith( string otherString )

Returns a Boolean value that indicates whether the current string ends with the specified otherString.

  1. bool Contains( string otherString)

Returns a Boolean value that indicates whether the current string contains the specified otherString anywhere.

  1. 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.

  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.

  1. static bool IsNullOrEmpty( string value)

It determines whether the given string value is null or empty (“”).

  1. static bool IsNullOrWhiteSpace( string value)

It determines whether the given string value is null or white space (“ “).

  1. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Constructors of ‘String’

A
  1. String( char[] )

It initializes the string with specified char[].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

StringBuilder

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Constructors of ‘StringBuilder’

A
  1. StringBuilder( )

It initializes the StringBuilder type of object with the default capacity (16).

  1. StringBuilder( int capacity )

It initializes the StringBuilder type of object with the specified capacity.

  1. StringBuilder( string value )

It initializes the StringBuilder type of object with the specified value.

  1. StringBuilder( string value, int capacity )

It initializes the StringBuilder type of object with the specified value and capacity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Properties of ‘StringBuilder’

A
  1. int Length { get; set; }

This property gets / sets the length (number of characters) in the string builder.

  1. char [int index] { get; set; }

This indexer gets / sets the single character at the specified character index.

  1. 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.

  1. int MaxCapacity { get; }

This property represents maximum number of characters up to which, the Capacity can be extended.

Default is int.MaxValue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Methods of ‘StringBuilder’

A
  1. string Append(string value)

Adds the given string value at the end of current value of string builder.

  1. 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.

  1. 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.

  1. string Replace( string oldString, string newString )

Returns a string after replacing all occurrences of oldString with newString.

  1. string ToString()

Returns the current value of string builder as a string object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When to use ‘String’

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When to use ‘String Builder’

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

DateTime

A

The System.DateTime is a structure that represents date and time value.

Default format: yyyy-MM-dd hh:mm:ss.fff tt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

‘DateTime’ Constructors

A
  1. 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.

  1. int Month { get; }

It returns the month (1 to 12) of current date & time value.

  1. int Year { get; }

It returns the year (1 to 9999) of current date & time value.

  1. int Hour { get; }

It returns the hour (0 to 23) of current date & time value.

  1. int Minute { get; }

It returns the minute (0 to 59) of current date & time value.

  1. int Second { get; }

It returns the second (0 to 59) of current date & time value.

  1. int Millisecond { get; }

It returns the millisecond (0 to 999) of current date & time value.

  1. int DayOfYear { get; }

It returns the day of year (1 to 366) based on current date & time value.

  1. DayOfWeek DayOfWeek { get; }

It returns the day of week - Sunday to Saturday (0 to 6) based on current date & time value.

  1. static DateTime Now { get; }

It returns an instance of DateTime structure that represents the current system date & time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

DateTime Methods

A
  1. string ToString()

It returns the date & time value in the default date format, based on current Windows settings.

  1. string ToString(string format)

It returns the date & time value in the specified format.

  1. 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

  1. 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

  1. 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

  1. 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

  1. static int DaysInMonth(int year, int month)

It returns number of days in the specified month in the specified year.

  1. 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”

  1. 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.

  1. 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.

  1. TimeSpan Subtract(DateTime value)

It returns an instance of TimeSpan structure, that represents date difference between the current instance and given date value.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. DateTime AddMilliseconds(double value)

It creates and returns a new instance of DateTime structure, after adding specified milliseconds to the current date & time value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Math

A

The System.Math is a static class that provides built-in methods for perform common mathematical operations with numbers.

Example:

Math.Pow(2, 3) //returns the value of 2 power 3

16
Q

Properties of ‘Math’ class

A
  1. const double PI = 3.1415926535897931

It returns the value of mathematical π value as constant, which represents ratio of the circumference of a circle to its diameter.

17
Q

Methods of ‘Math’ class

A
  1. static double Pow(double x, double y)

It returns the value of x power y.

  1. static double Min(double val1, double val2)

It returns the minimum (smaller) among given two numbers.

  1. static double Max(double val1, double val2)

It returns the maximum (bigger) among given two numbers.

  1. static double Floor(double value)

It rounds-down the given number. Eg: 10.29 becomes 10.

  1. static double Ceiling(double value)

It rounds-up the given number. Eg: 10.29 becomes 11.

  1. static double Round(double value)

It rounds up / down the given number, to the nearest integer value.

Eg: 10.29 becomes 10 and 10.59 becomes 11.

  1. static double Round(double value, int decimals)

It rounds up / down the given number, to the specified fractional digits.

Eg: Math.Round(value, 2)

10.272 becomes 10.27 and 10.275 becomes 10.38.

  1. static int Sign(double value)

It returns:

-1: if the value is a negative value

0: if the value is equal to 0

1: if the value is a positive value

  1. static double Abs(double value)

It returns the positive value of given number.

  1. static int DivRem(int val1, int val2, out int result)

It returns the quotient value of val1/val2; and provides the remainder value as ‘result’ out parameter.

  1. static double Sqrt(double value)

It returns square root value of given number.

18
Q

Regular Expressions

A

Regular Expression is a pattern that contains set of conditions of a string value.

Example: ^[a-zA-Z ]*$ - for alphabets and spaces on

The ‘Regex’ class represents regular expression to check whether the string value matches with specified pattern or not.

Useful for validations.

19
Q

Encoding

A

It is a concept that tells you represent characters into numbers (or any other specific format).

ASCII (American Standard Code for Information Interchange)
Each character occupies 7 bits (generally considered as 1 byte)
128 characters (0 to 127)
Includes all keyboard characters with alphabets, digits, special characters etc.

UTF-16 / Unicode (Universal Code)
Each character occupies 2 or 4 bytes (generally considered as 2 bytes)
About 144697 characters (approx)
Includes all natural language characters along with ASCII.

20
Q

‘System.IO’ namespace

A

This namespace contains classes to perform File I/O operations.

System.IO:
class File
class DriveInfo
class FileStream
class Directory
class FileNotFoundException
class FileInfo
class StreamWriter
class BinaryWriter
class DirectoryInfo
class StreamReader
class BinaryReader

21
Q

Exception Handling

A

Exception is a run time error occurs while executing the application.

When exception occurs, the current application terminates abruptly.

Exception Handling avoids abrupt termination of the application, in case of exception.

22
Q

Exception Handling

A

When CLR is unable to execute a statement, it is treated as exception.

‘try’ and ‘catch’ blocks are mandatory.

‘finally’ block and multiple ‘catch’ blocks are optional.

“try” block contains all the actual code, where exceptions may occurs.

Multiple “try” blocks for one catch block is not allowed. Nested “try” blocks is allowed

“catch” block contains error handling code; it executes only when a particular type of exception is raised during the execution of “try” block. Multiple “catch” blocks is allowed.

“finally” block executes after successful completion of “try” block; or after any catch block. It is optional.

“throw” keyword is used to throw built-in or custom exceptions, in case of invalid values found.

23
Q

FormatException

A

FormatException represents an error when it is unable to convert a string to a number, as the string contains characters other than digits (such as alphabets, spaces etc.).

Occurs when calling methods of ‘Convert’ class or ‘Parse’ method of numeric types with incorrect string value.

It’s a bad practice to throw FormatException implicitly / explicitly.

24
Q

Constructors of ‘FormatException’

A
  1. FormatException()

It initializes nothing.

  1. FormatException(string message)

It initializes ‘Message’ property.

  1. FormatException(string message, Exception innerException)

It initializes ‘Message’ and ‘InnerException’ properties.

25
Q

IndexOutOfRangeException

A

IndexOutOfRangeException represents an error when an index was supplied outside the available range to an array.

Occurs when accessing an array with a wrong index value, which is less than 0 or greater than or equal to size of the array.

It’s a bad practice to throw IndexOutOfRangeException implicitly / explicitly.

throw new IndexOutOfRangeException(…)

26
Q

NullReferenceException

A

NullReferenceExeption represents an error when you try to access a property, indexer or method through a reference variable when its value is null.

It’s a bad practice to throw NullReferenceException implicitly / explicitly.

throw new NullReferenceException(…)

27
Q

ArgumentNullException

A

ArgumentNullException represents an error when a null value is passed as argument to a method; and that method doesn’t accept null’s into that parameter.

It’s a good practice to throw ArgumentNullException implicitly / explicitly.

28
Q

ArgumentOutOfRangeException

A

ArgumentOutOfRangeException represents an error when a numeric value is passed as argument to a method; and it outside the acceptable range of values accepted by that method.

It’s a good practice to throw ArgumentOutOfRangeException implicitly / explicitly.

29
Q

ArgumentException

A

ArgumentException represents an error when the argument value of a parameter is invalid as per any validation rules / requirements.

It’s a good practice to throw ArgumentException implicitly / explicitly.

30
Q

InvalidOperationException

A

InvalidOperationException represents an error when you call a method; and it is invalid to call it a per current state of the object.

It’s a good practice to throw InvalidOperationException implicitly / explicitly.

31
Q

Custom Exception class

A

Custom Exception class is a class that is derived from any one of the exception classes (such as System.Exception or any other exception class).

If none of the pre-defined exception class meets your requirement, you will create an user-defined exception class (custom exception class).

All exception class in .net are derived from System.SystemException class.

System.SystemException is derived from System.Exception class.

Custom Exception classes should NOT be inherited from ApplicationException.