Solved

Easy Question I think

Posted on 2003-10-26
5
209 Views
Last Modified: 2010-04-16
How come some methods like: .ToLower() don't require a namespace but one such as: .WriteLine() does?
0
Comment
Question by:chilled2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:smitty22
ID: 9623918
ToLower operates on string objects, while WriteLine is a static method that is part of the Console class.  The full specification of WriteLine is
  System.Console.WriteLine().
This is because Console is a member of the System namespace.  However, since System is usually always included as a using directive, you can just write Console.WriteLine().

String is also part of the System namespace, but the ToLower method is not static -- in other words, it operates on instantiated members of the String class.  This is why you can do things like
  string s = "Hello";
  s.ToLower();
--BUT NOT--
  String.ToLower();


0
 
LVL 1

Expert Comment

by:kellycoinguy
ID: 9623928

String.ToLower is in System.String
Console.WriteLine is in System.Console
both in the System namespace.

I believe if you put:
using System;

at the top of your file, you shouldn't need to specify a namespace just the object:

Console.Writeline("..."); // this works fine for me

Can you be more specific about what you are required to do here?

-Kelly
0
 

Author Comment

by:chilled2003
ID: 9624228
Not really required to do anything just trying to learn C# :)  If .ToLower(); is in the System namespace then how come I cant put:

System.String.ToLower();
or
String.ToLower();

If it's part of System then how come if I dont include System as a namespace it still works with just .ToLower(); ?  But if I do that with WriteLine(); it errors and I have to put System.Console.WriteLine();
0
 
LVL 2

Accepted Solution

by:
smitty22 earned 50 total points
ID: 9624354
.ToLower belongs to the String class, which in turn belongs to the System namespace.  You can use string methods without a "using System;" directive because they are an exception -- they are handled almost like a primitive type such as int or double.  The "using System;" directive is implied when working with strings as primitive types.

For instance, without a using System directive you can do:
  string str = "this is a string";  // note lowercase "string"
  System.Console.WriteLine( str.ToLower() );  // or any other string method
BUT NOT
  String s = new String("this is not valid");  // compiler error: "missing a using directive"
0
 

Author Comment

by:chilled2003
ID: 9624368
Thanks, that cleared it up for me :)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question