Results 1 to 9 of 9

Thread: Why System. when creating new objects?

  1. #1

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80

    Why System. when creating new objects?

    Quick question!

    Can someone explain why you would define a new object as such(using Datatable as example):

    System.Data.DataTable dt = new DataTable();

    ..Instead of..

    DataTable dt = new DataTable();

    What's the difference????

    Thanks in advance!!!

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    There is no difference.
    If you are

    using System.Data;

    in your class you no longer have to type that out.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80
    That's what I suspected. I've seen some examples where the developer is 'using System.??' and still defines objects as 'System.??.object.

    Hmmm!! A little overkill maybe??

    Thanks!!

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    It happens. Sometimes you forget you Imported the namespaces. Or you copy and pasted some code from somewhere that had it verbose like that and didnt change it. No big deal either way. Dont think it changes performance in any way.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    New Member
    Join Date
    Aug 2004
    Posts
    5
    Originally posted by Cander
    In Soviet Russia, applications program you!
    Cander, in Soviet Russia 99% of people didn't use apps. however Government programmed people

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Overkill no doubt, but for a good reason, sometimes, especially when code is generated. Cander is right that it does not change performance - it's just a shortcut so we don't have to type so much.

    The good reason is to avoid ambiguity. Even the code, for example, when you add a button in the IDE, looks like this in a Windows Form project
    Code:
    private System.Windows.Forms.Button btnCancel;
    VS automatically adds the
    Code:
    using System.Windows.Forms;
    so it would seem redundant. Problem might be, what if I decide to make my own Button class? When it's fully qualified, ambiguity is not a problem.

    Mike

  7. #7
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Right - there are even a few ambiguously named classes within the .NET Framework itself (in System.Drawing vs System.Windows.Form, IIRC)
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    I always add the usings I need manually to the class anyway. I dont assume everyone that may use a class I wrote will be using VS. So its good that the class is self sufficient and not depending on VS specific files.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80
    Thanks for your input to all. I agree!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width