Lesson 2 Introduction To C
Lesson 2 Introduction To C
Why C#?
C# has many other reasons for being popular and in demand. Few of the reasons are
mentioned below:
Widely used for developing Desktop and Web Application: C# is widely used for
developing web applications and Desktop applications. It is one of the most popular
languages that is used in professional desktop. If anyone wants to create Microsoft
apps, C# is their first choice.
Community: The larger the community the better it is as new tools and software will be
developing to make it better. C# has a large community so the developments are done
to make it exist in the system and not become extinct.
Programming in C#:
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.
Advantages of C#:
Disadvantages of C#:
C# IDE
C# Install
Once the Visual Studio Installer is downloaded and installed, choose the .NET workload
and click on the Modify/Install button:
After the installation is complete, click on the Launch button to get started with Visual
Studio.
Visual Studio will automatically generate some code for your project:
C# Syntax
In the previous chapter, we created a C# file called Program.cs, and we used the
following code to print "Hello World" to the screen:
Program.cs
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Result:
Hello World!
Example explained
Line 2: A blank line. C# ignores white space. However, multiple lines makes the code
more readable.
Line 3: namespace is used to organize your code, and it is a container for classes and
other namespaces.
Line 4: The curly braces {} marks the beginning and the end of a block of code.
Line 5: class is a container for data and methods, which brings functionality to your
program. Every line of code that runs in C# must be inside a class. In our example, we
named the class Program.
C# Output
System.Console.WriteLine() OR
System.Console.Write()
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
Console.WriteLine("C# is cool");
}
}
}
C# is cool
Difference between WriteLine() and Write() method
Let's take at a look at the example below to understand the difference between these
methods.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
Console.WriteLine("Prints on ");
Console.WriteLine("New line");
Console.Write("Prints on ");
Console.Write("Same line");
}
}
}
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
int val = 55;
Console.WriteLine("Hello " + "World");
Console.WriteLine("Value = " + val);
}
}
}
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
int firstNumber = 5, secondNumber = 10, result;
result = firstNumber + secondNumber;
Console.WriteLine("{0} + {1} = {2}", firstNumber,
secondNumber, result);
}
}
}
5 + 10 = 15
C# Input
In C#, the simplest method to get input from the user is by using the ReadLine() method
of the Console class. However, Read() and ReadKey() are also available for getting
input from the user. They are also included in Console class.
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string testString;
Console.Write("Enter a string - ");
testString = Console.ReadLine();
Console.WriteLine("You entered '{0}'", testString);
}
}
}
using System;
namespace UserInput
{
class MyClass
{
public static void Main(string[] args)
{
string userInput;
int intVal;
double doubleVal;
Console.Write("Enter integer value: ");
userInput = Console.ReadLine();
/* Converts to integer type */
intVal = Convert.ToInt32(userInput);
Console.WriteLine("You entered {0}",intVal);