Table Of ContentC# Programming
Quickly Learn C# Programming
Copyright ©
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted
in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise,
without the prior written permission of the publisher.
Disclaimer
All the material contained in this book is provided for educational and informational purposes only. No
responsibility can be taken for any results or outcomes resulting from the use of this material.
While every attempt has been made to provide information that is both accurate and effective, the author
does not assume any responsibility for the accuracy or use/misuse of this information.
Table of Contents
C# Programming
Table of Contents
Introduction
Chapter 1: C# 101
Chapter 2: First Program and Basic Syntax
Chapter 3: Variables and Data Types
Chapter 4: Operators
Chapter 5: Conditional Statements
Chapter 6: Loops
Chapter 7: Understanding Methods
Chapter 8: Using Arrays
Chapter 9: Exception Handling
Chapter 10: Inheritance
Conclusion
Introduction
It’s fascinating to see how numbers, letters, and symbols come together to power our computers. If you
have always had an idea for a computer program, now is your time to learn to code. With C#, your
dreams of becoming a programmer can become true. And they can do so without frustration as C# is
easy to understand.
In this book, you will learn the basics of coding in C#. The book has been written in a way to be
understood by even those with zero programming skills. If you have always thought that coding was
difficult, this book will prove you wrong. Inside, there are lots of examples to deepen your
understanding. And by the time you hit the last page, you should be able to create simple C# programs.
With that, we can get started. If you haven’t already, fire up your computer. And if you don’t understand
some things at the beginning of the book, just keep going—things will make sense the further you go.
Chapter 1: C# 101
C#, pronounced as “C sharp,” is a general purpose object-oriented programming language. Some of its
strengths are that it is easy to learn, creates efficient programs, and can be compiled on a number of
systems. It is part of the .Net framework. And for this reason, some refer to it as the .Net language. C#
was developed by Microsoft, thanks to one Anders Hejlsberg.
With the name sounding like C and C++, you would expect C# to resemble those two aforementioned
languages. And that is the case. But C# goes on to also mimic most of what Java has to offer.
If you already have an understanding of C or C++, C# will prove to be easy.
With C#, you have freedom to create programs that C, C++, Java, and other high-level programming
languages can produce. You can create games, Windows applications, web applications, and apps for
iOS and Android.
Setting Up the Right Environment
In order to create C# programs, you need to have the right tools. Your computer is the most important
tool in this environment. But you will also need other things on the software side.
.Net Framework
I already said that C# is part of the .Net Framework. As such, your computer needs this in order to
execute C# code. If you are on a Windows computer, then you probably already have the .Net
Framework.
Integrated Development Environment (IDE)
Microsoft offers Visual Studio (VS) as the IDE for creating C# applications and other languages like
C++. With VS, you can create just about any kind of application you can imagine. VS can be
downloaded from the Microsoft website for free.
C# on Mac OS and Linux
The .Net framework is not available on Mac OS and Linux. Thankfully, there is an open-source
alternative called Mono. And it comes with a C# compiler. Visit the Go Mono website for more on this.
Chapter 2: First Program and Basic Syntax
Hello, Word!
As with every programming language, we will get our hands wet with the Hello World program. Know
that the examples in this book use Microsoft Visual C# 2010 Express, a trimmed version of Visual
Studio.
First, launch Visual C#. Click File, select New Project, and then Console Application. In my case,
this opens an editor in a file named Program.cs.
Since there is some code in the editor, hit F5 to run it. A window will quickly open and close. That’s
because the code does not have anything specific to do. We need to add more to it to create a “Hello
World” program. Here is how that will look:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// my first program in C#
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
}
Notice the three lines I have added? The one in green, the one right below it, and the one that reads
Console.ReadLine();. If you hit F5 again, you should get this output:
Hello, World!
Explanation
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using is a keyword employed to import a namespace. In this case, it imports the System namespace and
other sub-namespaces of System.
Now you may ask, what is a namespace? This refers to a construct that is used to organize code. Code
that is similar in functionality is grouped together, ensuring that there is no conflict. The most important
namespace is the System.
namespace ConsoleApplication1
This line contains the namespace declaration, which in this case is ConsoleApplication1.
class Program
class is another keyword that introduces the class declaration. After the class keyword, you can then
write the class name, which in this case is Program. Every program in C# needs at least one class. A
class contains data and methods for your program.
Code contained in a class must be enclosed in curly brackets.
static void Main(string[] args)
This is the entry point of this program, meaning this is the first piece of code the program will execute.
static means that the method can be accessed without an instance of the class. And void means what the
method will return, which in this case is nothing. Every program must contain a method called Main;
otherwise, the program will not run.
// my first program in C#
This is a comment. And the compiler will ignore it. Comments can help you remember what your code
does if you forget. And they will also tell others what you intend to achieve with your code. The double
slash at the beginning means the comment is limited to one line. If you have a comment that takes
several lines, write your comment inside the slashes (/*comments spanning several lines need to be
formatted this way*/).
Console.WriteLine("Hello, World!");
Console.WriteLine gives you the ability to output data in the console window. The data to display is
enclosed in parentheses.
Console.ReadLine();
When the program executes the Console.WriteLine statement, a console window will print “Hello,
World!” Without Console.ReadLine, that window will close as quickly as it opens: you won’t even see
your text. Console.ReadLine tricks your program into keeping the console window open until you
provide an input, like pressing the Enter key.
You might have noticed that our code contains blank lines and white spaces. These are there to make the
code readable.
You must know that C# is case sensitive. So Main and main are not the same. Also know that after
every statement or expression, you must write a semicolon (;).
If for some reason you do not want to use Visual C#, then you can code in a text editor. Here is how you
can create a Hello World program:
• Copy the Hello World code into a text editor like Notepad.
• Save the file as helloworld.cs
• Open Command Prompt and navigate to where you saved your file.
• Type csc helloworld.cs
• If everything is right, command line will create an executable file of your program.
• Type helloworld to run it.
Identifiers and Keywords
Identifiers
These are names you assign to namespaces, classes, variables, methods, etc. You can use letters,
numbers, and underscores to create identifiers. However, an identifier can only begin with a letter or an
underscore. If you would like to use a keyword as an identifier, then include @ at the beginning (e.g.
@class).
Here are other examples of valid identifiers:
• FIRST
• first
• fir_st
• FirstName
• \123names