Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Object-Oriented Programming with C#

You're reading from   Hands-On Object-Oriented Programming with C# Build maintainable software with reusable code using C#

Arrow left icon
Product type Paperback
Published in Feb 2019
Publisher Packt
ISBN-13 9781788296229
Length 288 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Abhishek Sur Abhishek Sur
Author Profile Icon Abhishek Sur
Abhishek Sur
Raihan Taher Raihan Taher
Author Profile Icon Raihan Taher
Raihan Taher
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Overview of C# as a Language FREE CHAPTER 2. Hello OOP - Classes and Objects 3. Implementation of OOP in C# 4. Object Collaboration 5. Exception Handling 6. Events and Delegates 7. Generics in C# 8. Modeling and Designing Software 9. Visual Studio and Associated Tools 10. Exploring ADO.NET with Examples 11. New Features in C# 8 12. Understanding Design Patterns and Principles 13. Git - The Version Control System 14. Prepare Yourself - Interviews and the Future 15. Other Books You May Enjoy

Writing your first C# program in a console application

As you are now aware of the fundamentals and basics of the C# language, literals, loops, conditions, and so on, I think it is time to see a C# code example. So, let's start this section by writing a simple console application, compiling it, and running it using the C# compiler.

Open any notepad application that you have in your computer and type in the following code:

using System;

public Program
{
static void Main(string[] args)
{
int num, sum = 0, r;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : " + sum);
Console.ReadLine();
}
}

The preceding code is a classic example of calculating the sum of all of the digits of a number. It takes a number as input using the Console.ReadLine() function, parses it, and stores it into a variable, num, loops through while the number is 0, and takes modulus by 10 to get the reminder of the division, which is then summed up to produce the result.

You can see there is a using statement at the top of the code block, which ensures that Console.ReadLine() and Console.WriteLine() can be called. System is a namespace from the code, which enables the program to call the classes defined inside it without specifying the full namespace path of the class.

Let's save the class as program.cs. Now, open the console and move it to the location where you have saved the code.

To compile the code, we can use the following command:

csc Program.cs

The compilation will produce something like this:

The compilation will produce program.exe. If you run this, it will take the number as input and produce the result:

You can see that the code is being executed in the console window.

If we dissect how the code is being executed further, we can see that the .NET framework provides the csc compiler, an executable that is capable of compiling my C# code into a managed executable. The compiler produces an executable with MSIL as its content, and then, when the executable is being executed, the .NET framework invokes an executable and uses JIT to compile it further so that it can interact with the input/output devices.

The csc compiler provides various command-line hooks, which can be used further to add dynamic link library (dll) references to the program, target the output as dll, and much more. You can find the full functional document at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-alphabetically.

You have been reading a chapter from
Hands-On Object-Oriented Programming with C#
Published in: Feb 2019
Publisher: Packt
ISBN-13: 9781788296229
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime