MOCKSTACKS
EN
Questions And Answers

More Tutorials








C# Create Method

In C#, A method is a group of statements that together perform a task or multiple tasks. Every C# program has at least one class with a method named Main, you create your own customized method to do specific tasks.

Methods in C# has an access modifier:

public: The type or member can be accessed by any other code in the same assembly or another assembly that references it. The accessibility level of public members of a type is controlled by the accessibility level of the type itself.
private: The type or member can be accessed only by code in the same class or struct.
protected: The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. In other words, internal types or members can be accessed from code that is part of the same compilation.
protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.
private protected: The type or member can be accessed by types derived from the class that are declared within its containing assembly.

Methods in C# return value

In C#, methods can return a value or not, you can sue keyword void to not return any value from the method.

Method in C# can be static

static means that the method belongs to the Program class and not an object of the Program class. If you are calling a method from another method which is static the new method should be also static.

C# create method

using System;

namespace MyApplication
{
  class Program
  {
  	static void printHelloWorld(){
        Console.WriteLine("Hello World!");
    }
    
    static void Main(string[] args)
    {
      printHelloWorld();
    }
    
  }
}

Output

Hello World!

Conclusion

In this page (written and validated by ) you learned about C# Create Method . What's Next? If you are interested in completing CS tutorial, your next topic will be learning about: CS Create Method With Parameters.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.