Vadim's Weblog

Never stop learning.

Archive for June, 2008

Reference Type interview questions.

Posted by Vadim on June 30, 2008

Part of my job is to interview developers for open position at my company.  I’m surprised how many times people give wrong answers to questions related to Reference type vs. Value type.  Bellow list of the questions some interviewers get incorrectly:

Question 1: Describe behavior of the code bellow:

   1: static void Main(string[] args)
   2: {
   3:   StringBuilder builder = null;
   4:   Foo(builder);
   5:   Console.WriteLine(builder.ToString());
   6: }
   7:  
   8: private static void Foo(StringBuilder builder)
   9: {
  10:   builder = new StringBuilder("Interview");
  11: }

Incorrect Answer 1: It will output “Interview”.

Correct Answer 1: NullReferenceException will be thrown on line 6.  Because builder is null and cannot access ToString() method.

Question 2: What will the output of the code bellow?

   1: static void Main(string[] args)
   2: {
   3:   StringBuilder builder = null;
   4:   Console.WriteLine(Foo(builder));
   5: }
   6:  
   7: private static bool Foo(StringBuilder builder)
   8: {
   9:   return builder is StringBuilder;
  10: }

Incorrect Answer 2: True

Correct Answer 2: False

Often people are very eager to demonstrate their knowledge of the subject and they volunteer statements like this one:

Reference Types live on the heap and Value Types on the stack.

And this triggers a question like this one from me:

   1: class A
   2: {
   3:   B b = new B();
   4:  
   5:   class B
   6:   {
   7:     public int i;
   8:   }
   9: }

Does it mean that in the code above b of type B will be stored on the heap but b‘s member i of type int is going to be stored on the stack?

This questions really confuses some people.

Correct statement is:

Reference Types and instance Value Types live on the heap and local Value Types live on the stack.

The purpose of this post is not to make fun but to educate.

kick it on DotNetKicks.com

Posted in .Net, C#, Reference Type | 3 Comments »

Take control of DateTime’s ToString() method.

Posted by Vadim on June 19, 2008

DateTime_ToString Today I had to write to the database a string that satisfies  following format: DDMMYYYY-CountryCode.  For example if DateTime object equal to 06/19/2008 and country code is USA, the value written to the database must be “06192008-USA“. 

The best way I found to do that is to use ToString() method of DateTime object with custom date and time format.

someDate.ToString("MMddyyy-") + CountryCode;

NOTE: In the format above I used two ‘M’s and two ‘d’s.  If only one ‘M’ and one ‘d’ was used, no month or date would be padded with zero.

someDate.ToString("Mdyyy-") + CountryCode;

The output for the code above would be “6192008-USA” assuming that the date is 06/19/2008 and the country code is USA.

UPDATE: I had to change the lines of code above because it had a bug.  Thanks to Alexey Romanov for pointing it out.  I put one line of code for the world to see and it was buggy.

The previous code looked like this:

someDate.ToString(String.Format("MMddyyy-{0}"), CountryCode);

Let assume that CountryCode is MEX.  In this case it’s the same as:

someDate.ToString("MMddyyy-MEX");

Remember that “M” is a special character for DateTime object.  The output for this code would be “06192008-6EX“.  It definitely is a problem.

Posted in .Net, C#, Coding, DateTime | 4 Comments »

Mock DataTable

Posted by Vadim on June 16, 2008

Today I had to work with some legacy code.  It was originally written in Delphi and then converted line by line to C#.  Before changing anything in the code I decided to create unit tests and make sure that I have 100% coverage.

One of the thing I had to do in my unit test is to mock DataTable.  Here are the steps I made during mocking.

  1. Create needed columns in a DataTable.
  2. Create a new DataRow.
  3. Assign values to the row.
  4. Finally add the row to the DataTable.

Repeat steps 2- 4 for each row you want to add to your DataTable.

Here’s the example:

   1: [Test]
   2: public void HolidayTest()
   3: {
   4:   MockRepository mocks = new MockRepository();
   5:   ICompanyDAL dalMock = mocks.CreateMock<ICompanyDAL>();
   6:   // Create DataTable
   7:   DataTable fakeHolidays = new DataTable();
   8:   // 1. Add Columns
   9:   fakeHolidays.Columns.Add("Holiday", typeof (DateTime));
  10:   fakeHolidays.Columns.Add("Name", typeof(string));
  11:   // 2. Create new DataRow
  12:   DataRow dayRow = fakeHolidays.NewRow();
  13:   // 3. Assign values to the row
  14:   dayRow["Holiday"] = DateTime.Parse("07/04/2008");
  15:   dayRow["Name"] = "Independence Day";
  16:   // 4. Add the row to the DataTable
  17:   fakeHolidays.Rows.Add(dayRow);
  18:   _pgDate.CompanyDal = dalMock;
  19:   using (mocks.Record())
  20:   {
  21:     Expect.Call(dalMock.GetHolidays()).Return(fakeHolidays);
  22:   }
  23:   using (mocks.Playback())
  24:   {
  25:     DataTable holidays = _pgDate.Holidays;
  26:     Assert.GreaterThan(holidays.Rows.Count, 0);
  27:   }
  28: }

I used Rhino.Mocks as my mocking framework in the example above.

kick it on DotNetKicks.com

Posted in .Net, C#, Coding, MbUnit, Rhino.Mocks, TDD | Tagged: , , | 2 Comments »

 
Follow

Get every new post delivered to your Inbox.