?
Solved

Easy Question I think

Posted on 2003-10-26
5
Medium Priority
?
215 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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