Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to Make a Calculator in C# ?
Next article icon

How to Execute C# Program on cmd (command-line)?

Last Updated : 30 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

C# is a general-purpose, modern and object-oriented programming language pronounced as “C sharp”. C# is among the languages for Common Language Infrastructure and the current version of C# is version 8.0. C# is a lot similar to Java syntactically and is easy for the users who know C, C++ or Java.

Since the C# is a lot similar to other widely used languages syntactically, it is easier to code and learn in C#.
Programs can be written in C# in any of the widely used text editors like Notepad++, gedit, etc. or on any of the compilers. After writing the program save the file with the extension .cs. There are various online IDEs such as GeeksforGeeks ide, CodeChef ide, etc. which can be used to run C# programs without installing. One can also use command-line options to run a C# program.

Sample C# Program to execute on Command-Line:




// C# program to print Hello World!
using System;
  
// namespace declaration
namespace HelloWorldApp {
  
// Class declaration
class Geeks {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // statement
        // printing Hello World!
        Console.WriteLine("Hello World!");
  
        // To prevents the screen from
        // running and closing quickly
        Console.ReadKey();
    }
}
}
 
 

Setting up the Environment for C# Compiler

Step 1: Go to Control Panel -> System and Security -> System. Under Advanced System Setting option click on Environment Variables as shown below:

Advanced System Settings

Setting up Environment variable
 

Step 2: Now, we have to alter the “Path” variable under System variables so that it also contains the path to the .NET Framework environment. Select the “Path” variable and click on the Edit button as shown below:

Environment-variable-setup-02-1
 

Step 3: We will see a list of different paths, click on the New button and then add the path where .NET Framework is installed.

CSharp-Command-Prompt-Environment-Setup
 

Step 4: Click on OK, Save the settings and it is done !! Now to check whether the environment setup is done correctly, open command prompt and type csc.

CSharp-Command-Prompt-02
 

Steps to Execute C# Program on cmd

Step 1: Open the text editor like Notepad or Notepad++, and write the code that you want to execute. Now save the file with .cs extension.

CSharp-Command-Prompt-00
 

Step 2: Compile your C# source code with the use of command:

csc File_name.cs

If your program has no error then it will create a filename.exe file in the same directory where you have saved your program. Suppose you saved the above program as Hello.cs. So you will write csc Hello.cs on cmd. This will create a Hello.exe file.

CSharp-Command-Prompt-03
 

Step 3: Now there are two ways to execute the Hello.exe. First, you have to simply type the filename i.e Hello on the cmd and it will give the output. Second, you can go to the directory where you saved your program and there you find filename.exe. You have to simply double-click that file and it will give the output.

  • Using Command:
    CSharp-Command-Prompt-04
  • Using .exe file:
    CSharp-Command-Prompt-05


Next Article
How to Make a Calculator in C# ?

A

Abhinav96
Improve
Article Tags :
  • C#

Similar Reads

  • How to Install a Font programmatically in C#?
    Fonts play a crucial role in the visual appearance of any application, and the right font can significantly enhance the user experience. You can do this directly in the application code, as I will demonstrate, but I will also show you how to do it when generating configuration files for your applica
    3 min read
  • C# Program to Get and Print the Command Line Arguments Using Environment Class
    In C#, Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that it retrieves command-line arguments information, exit codes information,
    2 min read
  • C# | How to play Beep sound through Console
    Given a normal Console in C#, the task is to play Beep sound through the Console. Approach: This can be achieved with the help of Beep() method of Console Class in System package of C#. The Beep() method of Console Class is used to play a Beep sound through the Console speaker. Syntax: public static
    2 min read
  • How to Make a Calculator in C# ?
    C# is an object-oriented, modern programming language that was created by Microsoft. It runs on the .NET Framework. C# is very close to C/C++ and Java programming languages. In this article, we will learn how to create a calculator in  C#.  Basic Functions of Calculator:Addition of two numbers.Diffe
    2 min read
  • How to Schedule a Thread for Execution in C#?
    In C#, we can schedule a thread for execution using the Thread class. The Thread.Start() method is responsible for scheduling a thread, allowing it to run when system resources are available. This method can be overloaded to pass parameters to the thread. Also, the Thread.Sleep() method can be used
    3 min read
  • C# Program to Get the Machine Name or Host Name Using Environment Class
    Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environme
    1 min read
  • How C# Code Gets Compiled and Executed?
    C# is a general-purpose, strongly typed, lexically scoped, functional, object-oriented, and component-oriented programming language. In this article, we are going to learn how C# code gets compiled and executed. Step-by-step process of C# code compilation: Step 1: Write a C# code. Step 2: Compile th
    4 min read
  • C# Program to Search Directories and List Files
    Given files and directories, now our task is to search these files and directories using C#. So to do this task we use the following methods: 1. SearchOption: This method is used to specify whether to search the current directory or the current directory with all subdirectories. Syntax: public enum
    2 min read
  • C# Program to Demonstrate the Use of GetCommandLineArgs() Method of Environment Class
    Environment Class provides information about the current platform and manipulates, the current platform. It is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environme
    3 min read
  • How to Read and Write a Text File in C#?
    Termination of a program leads to the deletion of all data related to it. Therefore, we need to store the data somewhere. Files are used for permanently storing and sharing data. C# can be used to retrieve and manipulate data stored in text files. Reading a Text file: The file class in C# defines tw
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences