Solved

Easy Question I think

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now