Book Image

Programming in C#: Exam 70-483 (MCSD) Guide

By : Simaranjit Singh Bhalla, SrinivasMadhav Gorthi
Book Image

Programming in C#: Exam 70-483 (MCSD) Guide

By: Simaranjit Singh Bhalla, SrinivasMadhav Gorthi

Overview of this book

Programming in C# is a certification from Microsoft that measures the ability of developers to use the power of C# in decision making and creating business logic. This book is a certification guide that equips you with the skills that you need to crack this exam and promote your problem-solving acumen with C#. The book has been designed as preparation material for the Microsoft specialization exam in C#. It contains examples spanning the main focus areas of the certification exam, such as debugging and securing applications, and managing an application's code base, among others. This book will be full of scenarios that demand decision-making skills and require a thorough knowledge of C# concepts. You will learn how to develop business logic for your application types in C#. This book is exam-oriented, considering all the patterns for Microsoft certifications and practical solutions to challenges from Microsoft-certified authors. By the time you've finished this book, you will have had sufficient practice solving real-world application development problems with C# and will be able to carry your newly-learned skills to crack the Microsoft certification exam to level up your career.
Table of Contents (22 chapters)
17
Mock Test 1
18
Mock Test 2
19
Mock Test 3

Creating a basic program in C#

Now we will look at how to create a basic program in C#. For the sake of explanation, we will work on the Console Application project:

  1. To create a new project, click on File | New Project and select Console App (.NET Framework) as the project type:

After giving the solution an appropriate name and path, click on OK. Check that the solution has been created. At this point, you should see the Solution Explorer. By default, a .cs file, Program.cs, should be added to the solution. By default, a method by the name of Main will also be added to the class. This method is the first entry point when this application is executed.

Please note that for a console program, it's not possible to change the default method, which would be the first entry point for the application.
  1. Let's open Program.cs at this stage. By default, the project will have the following using expressions for the following namespaces:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

A using statement basically signifies that the program can use the classes and methods defined in those namespaces for any execution. In further chapters, we will go over namespaces in detail and learn how to use them.

  1. Now, have a look at the program structure. By default, each class needs to be associated with a namespace. The namespace expression present in the Program.cs class indicates the namespace this class is part of:
Please note that C# is a case-sensitive language. This basically means that if we change the name of the method from Main to main, CLR will not be able to execute this method.

Each method in C# consists of two parts:

  • Input parameters: This is a list of variables that will be passed to the function when it's executed.
  • Return type: This is the value that will be returned by the function to the caller when the function finishes its processing.

In the case of the Program function declared previously, the input variable is a collection of arguments. The output variable is void; in other words, it does not return anything. In the forthcoming chapters, we will go over functions in more detail.

Now, let's write a program syntax to execute the famous Hello World output. In a console application, we can do this using Console.Writeline:

  1. The code implementation for this program is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
  1. At this stage, we have finished the program and are ready to execute it. Click on Build | Build Solution. Check that there are no compile time errors:
  1. At this stage, internally, Visual Studio should have created an .exe application for the project:
  1. Open Command Prompt and navigate directly to where the .exe file has been created. Execute the .exe file and check that the desired output of Hello World appears in Command Prompt.