Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:08:48+00:00 2026-06-12T07:08:48+00:00

The following examples are from the book Programming in the Key of C# .

  • 0

The following examples are from the book Programming in the Key of C#.

The first iteration of the program is the typical C way to do it, and the next reincarnation is more object oriented. The program is a simple example of calculating which day of the year a certain event occurred (december 31 is 365 or 366 if it’s a leap year).

using System;

class StructureAndMethodsTwo
{
    static void Main()
    {
        Date dateMoonWalk = new Date();

        dateMoonWalk.iYear = 1969;
        dateMoonWalk.iMonth = 7;
        dateMoonWalk.iDay = 20;

        Console.WriteLine("Moon walk: {0}/{1}/{2} Day of Year: {3}", 
            dateMoonWalk.iMonth, dateMoonWalk.iDay, dateMoonWalk.iYear,
            Date.DayOfYear(dateMoonWalk));
    }
}

struct Date
{
    public int iYear;
    public int iMonth;
    public int iDay;

    public static bool IsLeapYear(int iYear)
    {
        return iYear % 4 == 0 && (iYear % 100 != 0 || iYear % 400 == 0);
    }

    static int[] aiCumulativeDays = { 0, 31, 59, 90, 120, 151,
                                        181, 212, 243, 273, 304, 334 };

    public static int DayOfYear(Date dateParam)
    {
        return aiCumulativeDays[dateParam.iMonth - 1] + dateParam.iDay +
            (dateParam.iMonth > 2 && IsLeapYear(dateParam.iYear) ? 1 : 0);
    }
}

The next version of the program is identical except for the DayOfYear method which turns into

public int DayOfYear()
{
return aiCumulativeDays[iMonth -1] + iDay+ (iMonth > 2 && IsLeapYear(iYear) ? 1:0);
}

What’s exactly going on in the second version that makes it more OOP friendly than the first? Is an object of type Date being created by the method DayOfYear in the first iteration? I know that the instance version of the method has direct access to the fields of the structure, but I’m not aware of the distinct advantages of it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:08:49+00:00Added an answer on June 12, 2026 at 7:08 am

    In example two you are using the internal variables iYear, iMonth and iDay of the Date struct. The first example uses another copy of a Date object which you are passing to the DayOfYearFunction which is not needed.

    Edit:

    In the first example you use an instance of your Date struct and pass it as a parameter to the DayOfYear function, the instance of Date (dateParam) is allocated on the stack which would use more memory and require an additional garbage collection call from the CLR when it needs to be cleaned up.

    Performance issues: IMO, although slightly more memory is used, hardly any performance issues will occur due to the nature of the program. It is just a redundant parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following the examples from the book 'Programming Clojure', and I'm at page 17
I am working on my first login script. I have been following examples from
The following example is from the book 'Programming in Scala'. Given a class 'Rational'
I'm following the racetrack example from Jason Rudolph's book at InfoQ , using grails-1.2.1.
Im trying to hammer togehter a WYSIWYG-edit in c# following some examples from here
I took the following code from the examples page on Asio class tcp_connection :
Ive got the following code which I hacked together from website examples. It works
I'm reading a book about programming ASP.NET in C#. The book makes the following
This function is from the book Programming Collective Intelligence, and is supposed to calculate
I am reading about Dynamic programming in Cormen etc book on algorithms. following is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.