#1 When do you use structs vs classes?
- structs are value types that can contain data and functions
- structs are value types and do not require heap allocation.
- structs directly store their data in the struct, classes store a reference to a dynamically allocated object.
- structs are useful for small data structures
- structs can affect performance
- Constructors are invoked with the new operator, but that does not allocate memory on the heap
- A struct constructor simply returns the struct value itself (typically in a temporary location on the stack), and this value is then copied as necessary
- With classes, multiple variables may have a reference to the same object
- It is possible for operations on one variable to affect the object referenced by the other variable.
- With structs, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
- structs do not support user-specified inheritance, and they implicitly inherit from type object
call to functions |
#2 How Does One Parse XML Files?
- Reference System.Xml
- Use XmlReader to parse the text string
- You will need to have an idea of the XML elements and attributes for this example
- Use the XmlReader methods to read the XML data
#3 What is the difference between String and string ?
- No difference but code conventions state that upper case String should be used for class methods.
#4 How do I get the application’s path in a C# console app?
- Use reflection to get the executing assembly path
- Pass that the IO.Path.GetDirectoryName
#5 How do you call a base constructor in C#?
- Method 1 - Traditional
- Method 2 - New
private static string ModifyBase(string newName) { return newName.ToUpper(); }
#6 How would you count the occurrences of a string within a string?
- Single character search
- String search
- RegEx
#7 How to Check if a Number is a Power of 2?
- Use binary & operator
- Converts values to binary
- Performs logical AND operation on values
1000 - 8
0111 - 7
0000 – 0
1001 – 9
1000 – 8
1000 - 8
#8 What is the difference between break and continue in a loop?
- Break leaves immediately
- Continue allows loop to continue
- Place the word loop after each keyword to make sense of it
break loop
continue loop
#9 What is the difference between abstract and virtual functions?
- Abstract methods have no implementation and MUST be overridden
- Virtual methods MAY have an implementation but are not required to and then CAN be overridden.
- You cannot call base.method() for abstract but you can for virtual methods
#10 What is the difference between ref and out keywords?
- ref causes an argument to be passed by reference not by value. It provides a reference to the source value and changes made in the method will be made to the source object
- Arguments passed as ref must be initialized before they are passed.
- out also causes an argument to be passed by reference. Changes made to the parameter are also changed in the source.
- Arguments passed using out do not have to be initialized first.
- The called method must assign a value prior to the method return statement
#11 Explain how to encrypt/decrypt a string in .NET?
- Generate secret key
- Encrypt string
- Decrypt string using same key and encryption provider
#12 How do I get the index of the current iteration of a foreach loop?
- Maintain a counter and increment it manually
- Some collection types provide IndexOf()
#13 How do I get my own IP address in C#?
- Check private internal IP address
- Check public external IP address
- Use the DateTime class
- Get the current date
- Subtract the birth year from the current year
- Allow for actual birth month
#15 How do I get string value of an enum?
- Use Enum.GetName()
- Use ToString()
#16 How do I make a textbox that only accepts numbers?
- Use NumericUpDown
- Handle the KeyPress Event
- Handle the Text_Changed Event
#17 How do I round a decimal value to two places for output?
- Use string format specifiers
- ("#.##")
- ("{0:0.00}", value)
- ("n2")
- ("{0:c}", value)
#18 How do I remove duplicates from an array?
- Use LINQ
- Use a List
source: Microsoft MVA