0% found this document useful (0 votes)
43 views

Lesson 2 Introduction To C

C# is an object-oriented programming language developed by Microsoft for general-purpose programming. It is similar to Java and C/C++ syntactically. C# can be used to develop desktop and web applications, and is widely used for game development. It is easy to learn for programmers familiar with other languages. Visual Studio is a popular IDE for C# development that allows editing, compiling, and running C# programs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lesson 2 Introduction To C

C# is an object-oriented programming language developed by Microsoft for general-purpose programming. It is similar to Java and C/C++ syntactically. C# can be used to develop desktop and web applications, and is widely used for game development. It is easy to learn for programmers familiar with other languages. Visual Studio is a popular IDE for C# development that allows editing, compiling, and running C# programs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to C#

C# is a general-purpose, modern and object-oriented programming language


pronounced as “C sharp”. It was developed by Microsoft led by Anders Hejlsberg and
his team within the .Net initiative and was approved by the European Computer
Manufacturers Association (ECMA) and International Standards Organization (ISO).

C# is among the languages for Common Language Infrastructure and the current


version of C# is version 7.2. C# is a lot similar to Java syntactically and is easy for the
users who have knowledge of C, C++ or Java.

Why C#?

C# has many other reasons for being popular and in demand. Few of the reasons are
mentioned below:

Easy to start: C# is a high-level language so it is closer to other popular programming


languages like C, C++, and Java and thus becomes easy to learn for anyone.

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.

Game Development: C# is widely used in game development and will continue to


dominate. C# integrates with Microsoft and thus has a large target audience. The C#
features such as Automatic Garbage Collection, interfaces, object-oriented, etc. make
C# a popular game developing language.

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#:

 C# is very efficient in managing the system. All the garbage is automatically


collected in C#.
 There is no problem of memory leak in C# because of its high memory backup.
 Cost of maintenance is less and is safer to run as compared to other languages.
 C# code is compiled to a intermediate language (Common (.Net) Intermediate
Language) which is a standard language, independently irrespective of the target
operating system and architecture.

Disadvantages of C#:

 C# is less flexible as it depends alot on .Net framework.


 C# runs slowly and program needs to be compiled each time when any changes
are made.

C# IDE

 The easiest way to get started with C#, is to use an IDE.


 An IDE (Integrated Development Environment) is used to edit and compile code.
 In our tutorial, we will use Visual Studio Community, which is free to download
from https://visualstudio.microsoft.com/vs/community/.
 Applications written in C# use the .NET Framework, so it makes sense to use
Visual Studio, as the program, the framework, and the language, are all created
by Microsoft.

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.

On the start window, choose Create a new project:


Then click on the "Install more tools and features" button:
Choose "Console App (.NET Core)" from the list and click on the Next button:
Enter a name for your project, and click on the Create button:

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 1: using System means that we can use classes from the System namespace.

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.

Don't worry if you don't understand how using System, namespace and class works.


Just think of it as something that (almost) always appears in your program, and that you
will learn more about them in a later chapter.

Line 7: Another thing that always appear in a C# program, is the Main method. Any


code inside its curly brackets {} will be executed. You don't have to understand the
keywords before and after Main. You will get to know them bit by bit while reading this
tutorial.

Line 9: Console is a class of the System namespace, which has a WriteLine() method


that is used to output/print text. In our example it will output "Hello World!".

 Note: Every C# statement ends with a semicolon ;.


 Note: C# is case-sensitive: "MyClass" and "myclass" has different meaning.
 Note: Unlike Java, the name of the C# file does not have to match the class
name, but they often do (for better organization). When saving the file, save it
using a proper name and add ".cs" to the end of the filename. To run the
example above on your computer, make sure that C# is properly installed: Go to
the Get Started Chapter for how to install C#.
 The output should be: Hello World!

C# Basic Input and Output

C# Output

In order to output something in C#, we can use

System.Console.WriteLine() OR

System.Console.Write()

Here, System is a namespace, Console is a class within


namespace System and WriteLine and Write are methods of class Console.

Example 1: Printing String using WriteLine()

using System;

namespace Sample
{
class Test
{
public static void Main(string[] args)
{
Console.WriteLine("C# is cool");
}
}
}

When we run the program, the output will be:

C# is cool
Difference between WriteLine() and Write() method

The main difference between WriteLine() and Write() is that the Write() method only


prints the string provided to it, while the WriteLine() method prints the string and moves
to the start of next line as well.

Let's take at a look at the example below to understand the difference between these
methods.

Example 2: How to use WriteLine() and Write() method?

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");
}
}
}

When we run the program, the output will be:


Prints on
New line
Prints on Same line

 Combining (Concatenating) two strings using + operator and printing them


 Strings can be combined/concatenated using the + operator while printing.

Example 3: Printing Concatenated String using + operator

using System;

namespace Sample
{
class Test
{
public static void Main(string[] args)
{
int val = 55;
Console.WriteLine("Hello " + "World");
Console.WriteLine("Value = " + val);
}
}
}

When we run the program, the output will be:


Hello World
Value = 55

Example 4: Printing Concatenated string using String formatting

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);
}
}
}

When we run the program, the output will be:

5 + 10 = 15

Here, {0} is replaced by firstNumber, {1} is replaced by secondNumber and {2} is


replaced by result. This approach of printing output is more readable and less error
prone than using + operator.

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.

Example 5: Get String Input From User


using System;

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);
}
}
}

When we run the program, the output will be:

Enter a string - Hello World


You entered 'Hello World'

Difference between ReadLine(), Read() and ReadKey() method:

The difference between ReadLine(), Read() and ReadKey() method is:

1. ReadLine(): The ReadLine() method reads the next line of input from the


standard input stream. It returns the same string.
2. Read(): The Read() method reads the next character from the standard input
stream. It returns the ascii value of the character.
3. ReadKey(): The ReadKey() method obtains the next key pressed by user. This
method is usually used to hold the screen until user press a key.

Example 6: Reading Numeric Values from User using Convert class

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);

Console.Write("Enter double value: ");


userInput = Console.ReadLine();
/* Converts to double type */
doubleVal = Convert.ToDouble(userInput);
Console.WriteLine("You entered {0}",doubleVal);
}
}
}

When we run the program, the output will be:

Enter integer value: 101


You entered 101
Enter double value: 59.412
You entered 59.412

The ToInt32() and ToDouble() method of Convert class converts the string input to


integer and double type respectively. Similarly we can convert the input to other types.
Here is a complete list of available methods for Convert class.

You might also like