- Hello World — Introduction to C# interactive C# tutorial
- Run your first C# program
- Declare and use variables
- Work with strings
- Do more with strings
- Trim
- Replace
- Introduction
- Feedback
- C# Tutorial
- Examples in Each Chapter
- Example
- C# Exercises
- Learn by Examples
- C# Quiz
- My Learning
- Kickstart your career
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
Hello World — Introduction to C# interactive C# tutorial
This tutorial teaches you C# interactively, using your browser to write C# and see the results of compiling and running your code. It contains a series of lessons that begin with a «Hello World» program. These lessons teach you the fundamentals of the C# language.
To paste a code snippet inside the focus mode you should use your keyboard shortcut ( Ctrl + v , or cmd + v ).
Run your first C# program
Run the following code in the interactive window. Select the Enter focus mode button. Then, type the following code block in the interactive window and select Run:
Console.WriteLine("Hello World!");
Congratulations! You’ve run your first C# program. It’s a simple program that prints the message «Hello World!». It used the Console.WriteLine method to print that message. Console is a type that represents the console window. WriteLine is a method of the Console type that prints a line of text to that text console.
Let’s move on and explore more. The rest of this lesson explores working with the string type, which represents text in C#. Like the Console type, the string type has methods. The string methods work with text.
Declare and use variables
Your first program printed the string «Hello World!» on the screen.
As you explore C# (or any programming language), you’ll make mistakes when you write code. The compiler will find those errors and report them to you. When the output contains error messages, look closely at the example code, and the code in the interactive window to see what to fix. That exercise will help you learn the structure of C# code.
Your first program is limited to printing one message. You can write more useful programs by using variables. A variable is a symbol you can use to run the same code with different values. Let’s try it! Replace the code you’ve written in the interactive window with the following code:
string aFriend = "Bill"; Console.WriteLine(aFriend);
The first line declares a variable, aFriend , and assigns it a value, «Bill». The second line prints out the name.
You can assign different values to any variable you declare. You can change the name to one of your friends. Add these two lines in the interactive window following the code you’ve already added. Make sure you keep the declaration of the aFriend variable and its initial assignment.
Don’t delete the declaration of aFriend . Add the code below following the existing declaration.
aFriend = "Maira"; Console.WriteLine(aFriend);
Notice that the same line of code prints two different messages, based on the value stored in the aFriend variable.
You may have also noticed that the word «Hello» was missing in the last two messages. Let’s fix that now. Modify the lines that print the message to the following:
Console.WriteLine("Hello " + aFriend);
Select Run again to see the results.
You’ve been using + to build strings from variables and constant strings. There’s a better way. You can place a variable between < and >characters to tell C# to replace that text with the value of the variable.
If you add a $ before the opening quote of the string, you can then include variables, like aFriend , inside the string between curly braces. Give it a try:
Select Run again to see the results. Instead of «Hello «, the message should be «Hello Maira».
Work with strings
Your last edit was our first look at what you can do with strings. Let’s explore more.
You’re not limited to a single variable between the curly braces. Try this:
string firstFriend = "Maria"; string secondFriend = "Sage"; Console.WriteLine($"My friends are and ");
As you explore more with strings, you’ll find that strings are more than a collection of letters. You can find the length of a string using Length . Length is a property of a string and it returns the number of characters in that string. Add the following code at the bottom of the interactive window:
Console.WriteLine($"The name has letters."); Console.WriteLine($"The name has letters.");
This is a good time to explore on your own. You’ve learned that Console.WriteLine() writes text to the screen. You’ve learned how to declare variables and concatenate strings together. Experiment in the interactive window. The window has a feature called IntelliSense that makes suggestions for what you can do. Type a . after the d in firstFriend . You’ll see a list of suggestions for properties and methods you can use.
Do more with strings
You’ve been using a method, Console.WriteLine, to print messages. A method is a block of code that implements some action. It has a name, so you can access it.
Trim
Suppose your strings have leading or trailing spaces that you don’t want to display. You want to trim the spaces from the strings. The Trim method and related methods TrimStart and TrimEnd do that work. You can just use those methods to remove leading and trailing spaces. Try the following code:
string greeting = " Hello World! "; Console.WriteLine($"[]"); string trimmedGreeting = greeting.TrimStart(); Console.WriteLine($"[]"); trimmedGreeting = greeting.TrimEnd(); Console.WriteLine($"[]"); trimmedGreeting = greeting.Trim(); Console.WriteLine($"[]");
The square brackets [ and ] help visualize what the Trim , TrimStart and TrimEnd methods do. The brackets show where whitespace starts and ends.
This sample reinforces a couple of important concepts for working with strings. The methods that manipulate strings return new string objects rather than making modifications in place. You can see that each call to any of the Trim methods returns a new string but doesn’t change the original message.
Replace
There are other methods available to work with a string. For example, you’ve probably used a search and replace command in an editor or word processor before. The Replace method does something similar in a string. It searches for a substring and replaces it with different text. The Replace method takes two parameters. These are the strings between the parentheses. The first string is the text to search for. The second string is the text to replace it with. Try it for yourself. Add this code. Type it in to see the hints as you start typing .Re after the sayHello variable:
string sayHello = "Hello World!"; Console.WriteLine(sayHello); sayHello = sayHello.Replace("Hello", "Greetings"); Console.WriteLine(sayHello);
Two other useful methods make a string ALL CAPS or all lower case. Try the following code. Type it in to see how IntelliSense provides hints as you start to type To :
Console.WriteLine(sayHello.ToUpper()); Console.WriteLine(sayHello.ToLower());
Introduction
This specification is based on a submission from Hewlett-Packard, Intel, and Microsoft, that described a language called C#, which was developed within Microsoft. The principal inventors of this language were Anders Hejlsberg, Scott Wiltamuth, and Peter Golde. The first widely distributed implementation of C# was released by Microsoft in July 2000, as part of its .NET Framework initiative.
Ecma Technical Committee 39 (TC39) [later renamed to TC49] Task Group 2 (TG2) was formed in September 2000, to produce a standard for C#. Another Task Group, TG3, was also formed at that time to produce a standard for a library and execution environment called Common Language Infrastructure (CLI). (CLI is based on a subset of the .NET Framework.) Although Microsoft’s implementation of C# relies on CLI for library and run-time support, other implementations of C# need not, provided they support an alternate way of getting at the minimum CLI features required by this C# standard (see Annex C).
As the definition of C# evolved, the goals used in its design were as follows:
- C# is intended to be a simple, modern, general-purpose, object-oriented programming language.
- The language, and implementations thereof, should provide support for software engineering principles such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic garbage collection. Software robustness, durability, and programmer productivity are important.
- The language is intended for use in developing software components suitable for deployment in distributed environments.
- Source code portability is very important, as is programmer portability, especially for those programmers already familiar with C and C++.
- Support for internationalization is very important.
- C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions.
- Although C# applications are intended to be economical with regard to memory and processing power requirements, the language was not intended to compete directly on performance and size with C or assembly language.
The name C# is pronounced “C Sharp”.
The name C# is written as the LATIN CAPITAL LETTER C (U+0043) followed by the NUMBER SIGN # (U+0023).
Uncomment and update the date before submission to ECMA: ## COPYRIGHT NOTICE *© 2017 Ecma International* *This document may be copied, published and distributed to others, and certain derivative works of it may be prepared, copied, published, and distributed, in whole or in part, provided that the above copyright notice and this Copyright License and Disclaimer are included on all such copies and derivative works. The only derivative works that are permissible under this Copyright License and Disclaimer are:* 1. *works which incorporate all or portion of this document for the purpose of providing commentary or explanation (such as an annotated version of the document),* 1. *works which incorporate all or portion of this document for the purpose of incorporating features that provide accessibility,* 1. *translations of this document into languages other than English and into different formats and* 1. *works by making use of this specification in standard conformant products by implementing (e.g. by copy and paste wholly or partly) the functionality therein.* *However, the content of this document itself may not be modified in any way, including by removing the copyright notice or references to Ecma International, except as required to translate it into languages other than English or into a different format.* *The official version of an Ecma International document is the English language version on the Ecma International website. In the event of discrepancies between a translated version and the official version, the official version shall govern.* *The limited permissions granted above are perpetual and will not be revoked by Ecma International or its successors or assigns.* *This document and the information contained herein is provided on an «AS IS» basis and ECMA INTERNATIONAL DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY OWNERSHIP RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.»* —>
Feedback
Submit and view feedback for
C# Tutorial
C# (C-Sharp) is a programming language developed by Microsoft that runs on the .NET Framework.
C# is used to develop web apps, desktop apps, mobile apps, games and much more.
Examples in Each Chapter
Our «Try it Yourself» editor makes it easy to learn C#. You can edit C# code and view the result in your browser.
Example
using System; namespace HelloWorld < class Program < static void Main(string[] args) < Console.WriteLine("Hello World!"); >> >
Click on the «Run example» button to see how it works.
We recommend reading this tutorial, in the sequence listed in the left menu.
C# Exercises
Learn by Examples
Learn by examples! This tutorial supplements all explanations with clarifying examples.
C# Quiz
Learn by taking a quiz! The quiz will give you a signal of how much you know, or do not know, about C#.
My Learning
Track your progress with the free «My Learning» program here at W3Schools.
Log in to your account, and start earning points!
This is an optional feature. You can study W3Schools without using My Learning.
Kickstart your career
Get certified by completing the course
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.