Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Hello World!

C# Hello World - Your First C# Program

C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg.

In this tutorial, we will learn how to write a simple “Hello World!” program in C#. This will get you familiar with the basic syntax and requirements of a C# program.

The “Hello World!” program is often the first program we see when we dive into a new language. It simply prints Hello World! on the output screen.

The purpose of this program is to get us familiar with the basic syntax and requirements of a programming language.

“Hello World!” in C#

// Hello World! program
namespace HelloWorld
{
    class Hello {         
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}

When you run the program, the output will be:

Hello World!

How the “Hello World!” program in C# works?

Let’s break down the program line by line.

  1. // Hello World! Program

    // indicates the beginning of a comment in C#. Comments are not executed by the C# compiler.They are intended for the developers to better understand a piece of code.

  2. namespace HelloWorld{...}

    The namespace keyword is used to define our own namespace. Here we are creating a namespace called HelloWorld.

    Just think of namespace as a container which consists of classes, methods and other namespaces.

  3. class Hello{...}

    The above statement creates a class named - Hello in C#. Since, C# is an object-oriented programming language, creating a class is mandatory for the program’s execution.

  4. static void Main(string[] args){...}

    Main() is a method of class Hello. The execution of every C# program starts from the Main() method. So it is mandatory for a C# program to have a Main() method.

    The signature/syntax of the Main() method is:

    static void Main(string[] args){
        ...
    }
    
  5. System.Console.WriteLine("Hello World!");

    Here WriteLine() is a method of the Console class defined in the System namespace.

Alternative Hello World! implementation

Here’s an alternative way to write the “Hello World!” program.


// Hello World! program
using System;

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

Notice in this case, we’ve written using System; at the start of the program. By using this, we can now replace

System.Console.WriteLine("Hello World!");

with

Console.WriteLine("Hello World!");

This is a convenience we’ll be using in our later problems as well.

Task

Now that we have learned the basics of our code. Let’s solve a example in the editor below.

Write a code to print “Hello, InterviewBit!” (without quotes) in the editor below.

Start solving Hello World! on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test