Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm a little tired of the tutorials that tell us how to write code but do not breakdown into semi-retard language for us newbies.

Example:

Console.WriteLine(
    @"Please explain to me why I would ever use `Console`;
      give me a practicable eg. why I WOULD us the `.`, or why would I use `()` 
      after writing `WriteLine`");

or

string myString = // whatever comes after the `=`

Give me a piratical example as to when I would ever use this method.

In other words, is there something out there that will teach us how to write code by breaking down everything that we are writing and then give us a situation when this would be used and show us how to use it?

share|improve this question
Hey, im typing all this stuff down and have no idea when I would ever use it? is there a site with example programs with its code and therefore I can copy the code and slowly rebuild it one line at a time so I can see what im coding and how each line effects what im doing? – Bob Marley Aug 11 at 0:50
3  
Sorry, no tutorial will teach you how to think for yourself - though tutorials are largely regarded to be one of the worst ways to learn. Finding a reference site, or something that focuses on syntax elements might help, though will not tell you what situations to use it in. That's the hard part of learning to program, not the syntax. – Telastyn Aug 11 at 1:36
1  
You use the . and () because that's part of the syntax of the language. It's just something you have to use in order for the computer to know what it's supposed to do when it's executing your code. (or more accurately, in order for the compiler to know what to tell the computer to do when it's executing your code) – p.s.w.g Aug 11 at 2:25
While you could read APIs to get a handle on the keywords, the syntax of the language is something that comes from practice. In a way, one could ask why you use different punctuation in your question and white space? Well, it is to assist those of us that read it. The same can be said of the compiler requiring elements in how you type the code. Also, beware of typos as sometimes that can cause more issues as you have more than a few typos in the question. – JB King Aug 11 at 2:29
Question: is there a site with example programs with its code and therefore I can copy the code and slowly rebuild it one line at a time so I can see what im coding and how each line effects what im doing? Answer: ideone.com – rwong Aug 11 at 3:12

marked as duplicate by user16764, Dan Pichelman, MichaelT, Corbin March, Matthew Flynn Aug 11 at 4:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

C# code is arranged in classes.

Methods in classes are capable of being called without creating a "running copy" (what we call an "instance") of the class, if they are prefixed with the keyword static. We call these static methods.

public class Console
{
    public static void WriteLine(String s)
    {
        // code exists here to write to the console
    }
}

Console is a class. Its purpose is to provide methods that do things with the console window. WriteLine is one of those methods. Its purpose is to write a line of text to the console. WriteLine is a static method.

The rule about static methods is that you have to call them with the fully-qualified name of the class. Otherwise, how would the runtime know which class the method is on?

Console.WriteLine(...);
  ^          ^
  Class      static method

Were the WriteLine method not static, you would have to do this:

// Create instance of Console class
var console = new Console();

// Call WriteLine method
console.WriteLine(...);

Note the difference in casing; C# is a case-sensitive language. Console refers to the class definition, while console refers to an object instance of type Console.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.