Jump to content

Recommended Posts

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 !!

Link to comment
https://forums.phpfreaks.com/topic/177148-c/
Share on other sites

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); }
}

Link to comment
https://forums.phpfreaks.com/topic/177148-c/#findComment-934264
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/177148-c/#findComment-934281
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/177148-c/#findComment-934296
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/177148-c/#findComment-934349
Share on other sites

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??

Link to comment
https://forums.phpfreaks.com/topic/177148-c/#findComment-936616
Share on other sites

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#).

Link to comment
https://forums.phpfreaks.com/topic/177148-c/#findComment-936618
Share on other sites

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.  :shrug:

Link to comment
https://forums.phpfreaks.com/topic/177148-c/#findComment-936624
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.