Strings and Arrays
Q: What is string in C#?
A: In C#, a string is a sequence of characters used to
represent text. Strings in C# are represented by the
String class, which provides a range of methods for
string manipulation, such as searching, comparing,
concatenating, and modifying strings.
Q: List of methods or properties
for string. Without explaining.
A: Contains(), StartsWith(), EndsWith(), Join(), IndexOf(),
Insert(), Length, Remove(), Replace(), Trim(), Split(),
IsNullOrEmpty(), Format() etc.
Q: Describe difference between
string and System.String
A: In C#, the string keyword is an alias for String;
therefore, String and string are equivalent.
Q: What is the default value for string?
A: Default value for string is null.
Q: in C# string objects are immutable.
Could you explain what this means?
A: they can't be changed after they're created.
Q: What is String interpolation?
A: You declare Interpolated strings with the ‘$’ special
character. An interpolated string includes interpolated
expressions in braces. You can check more information here
https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/exploration/interpolated-strings
Q: How do you check if a string
is null or empty in C#?
A: You can check if a string is null or empty
using the string.IsNullOrEmpty(line); method
Q: How can we change type of int variable
to string?
A: We have method ToString();
Q: Imagine you should create string variable
with the following contents:
“
folder1/folder2/filename.txt
Mr Smith
”
All punctuation, brackets, spaces and line breaks
must be preserved. What you can use to do that?
A: You can use different literals like /r, /n etc., use
double slashes (//) instead one. But I would recommend
you for such kind of cases use @
https://learn.microsoft.com/en-us/dotnet/csharp/language-
reference/tokens/verbatim
Q: What is the difference between string and
StringBuilder? When is it recommended to use
StringBuilder instead of string and why?
A: String is immutable type. That means when we
modify string, we create new one. This may hit your
memory. StringBuilder is used to concatenation of
strings. StringBuilder is mutable.
Q: If you want remove all spaces in the line,
what will you do?
line = line.Replace(" ", string.Empty);
Q: What is the Array?
A: in C#, an array is defined as a collection
of elements of the same data type
Q: What is the difference between following arrays:
int[,] array1 = new int[4, 2];
int[][] array2 = new int[4][];
A: the second array is Jagged array. A jagged array is an
array whose elements are arrays, possibly of different
sizes. But the first one is like matrix or table, spreadsheet
where each rows have the similar size. The same for
columns.
Q: What is the output of this code?
A: IndexOutOfRangeException. Because
first index for C# collections is 0.
Q: Describe methods of Array in C#
A: Sort(), Reverse(), IndexOf(), LastIndexOf(), Clear() etc.
Q: What is the output of this code?
A: Output is “hello”
Q: What is the output of this code?
A: Output is “world”
Q: Why did the error occur?
A: We try to replace char by string.
Error: Cannot convert type string to char
Q*: What is the output of this code?
A: Output is:
Do not forget about self learning
Q*: How we can reverse string value?
A: There are some variants how we can do that:
1.Using a for loop:
You can create a new string by iterating through the
original string from the end to the beginning. Better to use
StringBuilder.
2. Your string variable to char array and use
Array.Reverse() method.
3. Using LinQ
Thank you!