viernes, 19 de septiembre de 2014

Twenty C# questions explained

#​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




#​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





#​What is the difference between String and string ?

  • No difference but code conventions state that upper case String should be used for class methods.




#​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



#​How do you call a base constructor in C#?

  • Method 1 - Traditional
public InheritTest(string newName) : base(newName) {     }  
  • Method 2 - New
public InheritTest(string name) : base(ModifyBase(name)) {     }  
private static string ModifyBase(string newName) {  return newName.ToUpper();  }









#​How would you count the occurrences of a string within a string?

  • Single character search
- foreach
  • String search
- RegEx

#​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


#​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

#​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
- Use Dns.GetHostEntry, IPAddress, and AddressList
  • Check public external IP address
- Make external calls


#​14 How do I calculate someone's age in C#?

  • 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


#​19 How do I sort a dictionary by value?


  • Use LINQ
  • Sorts into a new object


#​20 How do I return multiple values from a function in C#?


  • Using out parameters
  • Using arrays or structs
  • Consider finding min and max values in an array


source: Microsoft MVA