What is C# language?
C# (Pronounced as "C sharp") is a programming language designed by Microsoft to combine the power of C/C++ and the productivity of Visual Basic.
C# is a multi-purpose computer programming language suitable for a wide variety of development needs.
C# is a universal language that is used on many operating systems, including Microsoft Windows.
C# is one of the languages used in the Microsoft .NET Framework.
Structure of a C# Program
A C# program basically consists of the following parts:
Namespace declaration
A class
Class methods
Class attributes
A main() method
Statements and Expressions
Comments
namespace
The namespace keyword is used to declare a scope.
This namespace scope lets you organize code.
Within a namespace, you can declare one or more of the following types:
another namespace
class
interface
struct
enum
delegate
Whether or not you explicitly declare a namespace in a C# source file, the compiler adds a default namespace.
This unnamed namespace, sometimes called the global namespace, is present in every file.
class Program
class Program names the piece of code Program.
So, when we save or compile the code it will be named program.
main() method
The main method is where program starts execution.
Main is a method of class Program.
It is a special method that tells the compiler to start a program execution.
This is the point at which the start of the actual code begins.
Comments
Comments allow inline documentation of source code.
The C# compiler ignores comments.
The styles of comments allowed in C# are as follows: /* */, //.
Statements
A statement can declare a variable, define an expression, perform a simple action by calling a method, control the flow of execution of other statements, create an object, or assign a value to a variable, property, or field.
Difference between C++ and C#
C++
C#
It supports Object Oriented Programming
It is purely Object Oriented Programming
C++ code usually compiles to assembly language
C# by contrast compiles to Intermediate language (IL), which has some similarities to java byte code
It will allow multiple inheritance
It supports inheritance, but not multiple, only 1 override per method or function
It is platform dependent
It is platform independent
In C++, the main class is different. It is outside the class.
In C#, the main class is inside the namespace.
It uses the header files
It uses the namespaces
Compiling and Running your code
Start Visual Studio.
On the menu bar, go to File, New, Project.
Choose Visual C#, and then click Windows.
Choose Console Application.
Specify a name for your project, and then choose the OK button.
The new project appears in Solution Explorer.
Write code in the Code Editor.
Click the Run button or the F5 key to run the project.
A Command Prompt window appears that contains the line “Hello World”.
Example: Here is a simple example of “Hello World”.
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
Output
Hello World