Solved

Easy Question I think

Posted on 2003-10-26
5
206 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDependency to get update from sql to my c# app 2 66
Expression Evaluater 3 38
transaction in asp.net, sql server 6 43
"Emulate" TAB key when press Enter Key 3 49
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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