mlnsharma Posted October 10, 2009 Share Posted October 10, 2009 Hi I am a Novice in c#, Can any one explain me this ? { using System; // // // DateTime dt = DateTime.Now; Console.WriteLine("{0}",dt.year); } why is it not like { DateTime dt = new DateTime(); // Object Inititalization Console.WriteLine("{0}",dt.year); } What is "Now", Is it a class under DateTime. If it is a class why isnt it intialised as i did ? If it is a method under DateTime, How can it be assigned to an object of DateTime ? If it is an attribute under DateTime, How can it be assigned to object of DateTime ? Please Clarify me over this !! Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/ Share on other sites More sharing options...
cags Posted October 10, 2009 Share Posted October 10, 2009 What are you using to develop your C# code? If your using Visual Studio it would tell you what it is. I don't have it installed at the moment so I can't look, but off the top of my head it looks like Now is a static property that returns a data time object, with the value of the date/time of when the property was called. I haven't programmed C# in 18 months or so, as such this syntax won't be perfect, but hopefully it'll get the idea across. public static Now { get { return new DateTime(now); } } Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-934264 Share on other sites More sharing options...
KevinM1 Posted October 10, 2009 Share Posted October 10, 2009 Actually, DateTime is a struct, not a class. Now is a field of the struct representing the current system time. Also, class properties aren't static. Example: public class Person { private string name; private int age; public Person(string name, int age) { this.name = name; this.age = age; } public string Name { get { return this.name; } set { this.name = value; } } public int Age { get { return this.age; } set { this.age = value; } } } //... Person person = new Person("Bubba", 34); person.Name = "Forrest Gump"; Console.WriteLine("{0}'s age is {1}", person.Name, person.Age); Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-934281 Share on other sites More sharing options...
cags Posted October 10, 2009 Share Posted October 10, 2009 As I say it's a long time since I've done any C#. I guess I was trapped between a mis-match of ideas. Since it was being called without an instance of the object being created my first throught was a static method/function, but since it didin't have the parentheses I thought property. If DateTime is a struct (which does make more sense now I come to think of it), how does it posses a field that can be called to return an instance of DateTime? Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-934296 Share on other sites More sharing options...
KevinM1 Posted October 10, 2009 Share Posted October 10, 2009 As I say it's a long time since I've done any C#. I guess I was trapped between a mis-match of ideas. Since it was being called without an instance of the object being created my first throught was a static method/function, but since it didin't have the parentheses I thought property. If DateTime is a struct (which does make more sense now I come to think of it), how does it posses a field that can be called to return an instance of DateTime? In C#, it looks like it may be overloaded as both a struct and a class. VB makes a distinction between a Date object and instantiating it with the value obtained by DateTime.Now, but C# doesn't. See: http://msdn.microsoft.com/en-us/library/system.datetime.aspx Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-934349 Share on other sites More sharing options...
cags Posted October 10, 2009 Share Posted October 10, 2009 Ok, so I took a look at the msdn documentation you linked to... public static DateTime Now { get; } Source: http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx Does that not make Now a static Property? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-934381 Share on other sites More sharing options...
KevinM1 Posted October 10, 2009 Share Posted October 10, 2009 Haha, you're right. I didn't realize that properties could be static. Still learning the ins and outs of the language myself. Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-934385 Share on other sites More sharing options...
mlnsharma Posted October 14, 2009 Author Share Posted October 14, 2009 Ok, So for a structure,it might be this way in its definition struct DateTime { static (type) Now ; // Field Name-Now // Other members }Now; // Variable of type DateTime Say if i accessed DateTime dt; dt = DateTime.Now; So, is "Now", a static variable of type DateTime ? or a static variable in Structure DateTime ?? If it is a part of struct DateTime, then How can i assign the member of the structure to the structure variable ?? Can u clarify this too?? Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-936616 Share on other sites More sharing options...
corbin Posted October 14, 2009 Share Posted October 14, 2009 Now is a static variable (technically it would be called a member since it's a variable in a struct*) in the structure DateTime of the type (type). *Assuming you want to use the same terminology when talking about structs that you would use when talking about classes "How can i assign the member of the structure to the structure variable ??" Technically you cannot do this: struct Struct1 { Struct1 var; } But oddly enough the reason is not because of the recursiveness (technically all variables are really just pointers to memory, so it doesn't matter where it points or if it even points anywhere). The reason you can't do it is because the compiler doesn't yet know what Struct1 is while inside of Struct1. If my theory is correct, then technically it would be doable with a class since you can use prototypes with classes (well in C++ you can; don't know about C#). Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-936618 Share on other sites More sharing options...
cags Posted October 14, 2009 Share Posted October 14, 2009 I'd have thought with the nature of a property being what it is, it would get past any potential compiler issues. The Now property is essentially... public static DateTime Now { get { return NEW DateTime('passing in variables for actual time here'); } } So technically there is no 'variable' per se that holds the value. When the property is called a new class/struct is created and returned. But as always, I could be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/177148-c/#findComment-936624 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.