Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Documentation

What does static void mean?

Apparently, for example, Console.WriteLine is a static void method.

I can't get my head around this. I know void means it doesn't return anything, but what does that mean exactly? To me, the WriteLine method takes a string and prints it to the screen.

Please help me understand the various types of methods.

4 Answers

Steven Parker
Steven Parker
216,083 Points

You're right that the "WriteLine method takes a string and prints it to the screen". But think about that for a moment, after it prints to the screen, it returns control to the place in the program where it was called. But it does not give back a value. So the return type is "void" (nothing). It doesn't mean that it does nothing, it just returns nothing.

On the other hand the ReadLine method returns a string. When it returns to the caller, it gives this string back as the result of calling it.

Does that make sense now?

And all of this is totally unrelated to "static", that's something else entirely. Static means that there's only one of them, you don't need an instance to access it, and all instances (if any) will share the same one.

Hmm, I am sort of understanding it. Would you please be able to give me examples of a method that returns something? I can perhaps get a better understanding by way of contrast. And maybe something numerical in nature? I think the strings are confusing me.

Thank you!

Steven Parker
Steven Parker
216,083 Points

Returning numbers or strings is very similar, but here's an example with a number. The Count method returns how many things are in an iterable. So for example if you had a collection of flowers, you might use it to find out big the collection was, like this:

int howmany = flowers.Count();   // the Count() method returns the number of items

In this case, howmany is being assigned to the value returned by Count. If Count was void, there would be nothing to assign (and the statement would cause a compiler error).

Ranbir Singh
Ranbir Singh
1,566 Points

There are two types of methods, class methods that belongs to the class (Static) and other one belongs to instance of the class. in order to call Static method You have to write the name of the Class followed by a period and then method name whereas non static methods which are instance methods of the class are called or invoked using instance or object of the class.

The example below : I created a class names Example, which contains two methods Static method which is StaticMethod() and non static method NonStaticMethod();

Now to use them in the code I create an another class called Program with Main Method

first create an object of the class Example ex = new Example(); then use object to call the method

to call static method you just need to write the name of Class i.e Example.StaticMethod();

Thanks

public class Example { public static void StaticMethod() { // do something; }

puclic void NonStaticMethod() { //do something. }

}

public class Programe { public static void Main(){

 Example ex = new Example(); //create an instance or object of example class

// execute or call non static method using an object ex of Example Class

     ex.NonStaticMethod();

  //now execute Static Method using class name

    Example.StaticMethod();

} }

Lee Owen
Lee Owen
7,907 Points

A really simple way of thinking about void methods vs non void methods is like asking a friend to do something.

You can ask them to say what time it is and they can just say it out loud and that's the end of it, which would be a void method.

A non void method would be similar to if you were to ask a friend what time it is so that you could take that time and tell it to another friend so that he could tell you if he has anything scheduled for that time on his calendar.

If you see the static keyword it means that there is only one instance of it, and I'm sure that will get covered more in depth later on.

Robert Gioeli
Robert Gioeli
667 Points

You would think they could just make C# recognize when there is something to return at this point or not lol