In this program statement in the constructor method body is expressed in a different way. I wish to know whether this is unique for the DateTime only. Problem is highlighted.
using System;
namespace MethodOverloading
{
public class Time1
{
// private member variables
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second;
// public accessor methods
public void DisplayCurrentTime()
{
Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second);
}
// constructors
public Time1(DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
public Time1(int Year, int Month, int Date, int Hour, int Minute, int Second)
{
this.Year = Year;
this.Month = Month;
this.Date = Date;
this.Hour = Hour;
this.Minute = Minute;
this.Second = Second;
}
}
public class MethodOverloadingTester
{
public void Run()
{
DateTime currentTime = DateTime.Now;
Console.WriteLine(currentTime);
Time1 time1 = new Time1(currentTime);
time1.DisplayCurrentTime();
Time1 time2 = new Time1(2000, 11, 18, 11, 03, 30);
time2.DisplayCurrentTime();
}
static void Main()
{
MethodOverloadingTester t = new MethodOverloadingTester();
t.Run();
Console.Read();
}
}
}
/*
9/17/2008 8:25:28
11/18/2000 11:3:30
*/
Loading

VulpesPosted Dec 2, 2014, 8:27 AM
Instead of having to pass the fields individually, you pass them as a DateTime which conveniently packages them up into a single structure which you then decompose into its constituent parts when setting the fields.
MahaPosted Dec 2, 2014, 9:28 AM
VulpesPosted Dec 2, 2014, 9:22 AM
A 'tick' for this purpose is 100 nanoseconds.
Code within the DateTime structure converts these ticks to and from a normal date/time representation including years, months, days, hours, minutes, seconds and milliseconds.
This is much more compact than having to store these components individually.
MahaPosted Dec 2, 2014, 9:16 AM
How the following code is correctly selecting them.
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
VulpesPosted Dec 2, 2014, 8:48 AM
Those situations would certain include where you want to use the current date and time since, of course, this is conveniently available from the DateTime.Now property.
MahaPosted Dec 2, 2014, 8:40 AM
Michal HabalcikPosted Dec 2, 2014, 8:28 AM
Sibeesh VenuPosted Dec 2, 2014, 8:24 AM
There are several usages of this keyword in C#.
You can avoid the first usage by declaring getter and setter for all fields and accessing fields only through properties. In C# 3.0 this can be done easily via automatic properties however you lose the debugging advantage of this approach.
http://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword
Please go through that links and understand the usage of this operator.