Lecture #1 Introducing Visual C#

Introduction to According to , C# (pronounced “C sharp”) is a programming language that is the C# designed for building a variety of applications that run on the .NET Framework. C# is simple, Language and powerful, type-safe, and object-oriented. It provides many innovations to enable rapid the .NET application development while retaining the expressiveness and elegance of C-style languages. Framework C# was developed around 2000 by Microsoft within its .NET initiative and later approved as a standard by Ecma and ISO. Microsoft’s Visual Studio supports Visual C# with a full-featured code editor, compiler, project templates, designers, code wizards, a powerful and easy-to-use debugger, and other tools.

C# programs run on the .NET Framework, which is an integral component of Windows that includes a virtual execution called the (CLR) and a unified set of class libraries. The CLR is Microsoft’s implementation of the common language infrastructure (CLI). It is an international standard that is the basis for creating execution and development environments in which languages and libraries work together seamlessly.

Visual Studio Visual Studio 2017 is a rich, integrated development environment (IDE) for creating stunning 2017 and the applications for Windows, Android, and iOS, as well as modern web applications and cloud Developer services. It is a full-featured and extensible toolkit for developers to build Windows-based Command applications. As of January 2019, Microsoft says “Visual Studio Community is free for Prompt individual developers, open source projects, academic research, training, education, and small professional teams”. Students can, thus, download it from the https://www.visualstudio.com/downloads/ website.

The Developer Command Prompt for Visual Studio automatically sets the environment variables that enable developers to easily use the .NET Framework tools to hand-code applications. The Developer Command Prompt is installed with full or community editions of Visual Studio, although it is not installed with the Express versions of Visual Studio. Throughout this class, students will learn to hand-code C# programs and compile them with Visual Studio’s Developer Command Prompt. Later lectures will discuss about the C# programming basics in more detail.

Creating C# C# programmers write text-based source codes similar to the following example using any text source files editor such as the Microsoft Notepad, and then compile the source codes to object codes with C# compiler. The term “text-based” means the content consists of only a combination of English characters (alphabet, numerals, and symbols).

namespace Samples { class Welcome { static void Main(string[] args) { System.Console.Write("Welcome to CIS218 Visual C#!"); } } }

A “compiler” is a special program that reads statements written in the C# programming language and converts the “text-based” content into the binary “machine code” for the computer’s processor to read and execute. During compilation the compiler transforms source code into pieces of “object codes”. Each piece of object codes cannot be executed. The

Visual C# - Penn P. Wu, PhD. 1 compiler must further assemble object codes into one single executable application with the .exe extension in order to be executable in the Windows environment. The Windows operating systems will execute the completed .exe applications. By the way, each object code is known as an “” in the .Net platform. The following figure illustrates how the C# compiler converts a C# source code into an .exe application.

Object code 1 class Welcome 10011001100111 { Object code 2 10001111001111 00000011110010 } 000111110011 Object code n

C# source codes must be saved as and adopt the “.cs” file extension, such as “Welcome.cs”. Throughout this course, the instructor recommends students to use Microsoft Notepad as editor to write all the source codes. The following figure illustrates how to use Notepad to save the source code in a file named “Welcom.cs”, which will be saved as a generic text file. In a Notepad window, click File and then “Save As...” can lead to the following figure. Always make sure to the “Save as type:” is changed to “All Files (*.*); otherwise, Notepad might automatically add “.txt” to the file name (“Welcome.cs.txt”).

Another way to use Notepad to create a source file named “Welcome.cs” is by typing notepad.exe Welcome.cs (or notepad Welcome.cs) and pressing [Enter] in the Developer Command Prompt.

C:\Program Files\\2017\Community>notepad.exe Welcome.cs

In this course, the instructor recommends students to create a “C:\cis218” directory and save all the Visual C# source codes in that directory. The Developer Command Prompt supports the MS-DOS commands, such as “md” and “cd”. The “md” command can create a new directory, while the “cd” command can change from current directory to the specified directory. To create a new directory named “cis218” under the “root” directory of the “C” drive, type md c:\cis218 and press [Enter].

C:\Program Files\Microsoft Visual Studio\2017\Community>md C:\cis218

To change to that directory, type cd c:\cis218 and press [Enter].

C:\Program Files\Microsoft Visual Studio\2017\Community>cd C:\cis218 C:\cis218>

Throughout this course, students should learn to create a source file in the Developer Command Prompt by typing notepad.exe Welcome.cs (or simply notepad Welcome.cs) and press [Enter] in the prompt. Click “Yes” after.

C:\cis218>notepad Welcome.cs

In a Windows machine with Visual Studio installed successfully, programmers can invoke the C# compiler by typing its name, csc.exe, followed by options and the file name of source code

Visual C# - Penn P. Wu, PhD. 2 on the Developer Command Prompt. The following demonstrates how to compile the source code “Welcome.cs” to a self-executable program “Welcome.exe” by typing csc.exe Welcome.cs (or simply csc Welcome.cs) and press [Enter].

C:\cis218>csc Welcome.cs Microsoft (R) Visual C# Compiler version 1.0.0.50618 Copyright (C) Microsoft Corporation. All rights reserved.

During the compilation, the compiler creates a new file, Welcome.exe, which is a self- executable application. One way to check its existence is to type dir Welcome.* and press [Enter] in the prompt. The following shows how the output could be if Welcome.exe is created successfully. It is necessary to note the source code will stay the way it is, which means no change is made to its content, even after compilation.

C:\cis218>dir Welcome.* Volume in drive C has no label. Volume Serial Number is 84AF-8E2B

Directory of C:\cis218

01/01/2019 10:05 PM 875 Welcome.cs 01/01/2019 10.05 PM 13,824 Welcome.exe

After compilation, type Welcome.exe (or simply Welcome) and press [Enter] to run the .exe application.

C:\cis218>Welcome.exe

The above code, again, is a concole application. In Microsoft’s term, this Welcome.exe program’s “Output File Format” is set to be “DOS-based” which means it must be executed in a Command Prompt (or a DOS envrionment). This kind of program will not work in Windows desktop envrionment. The above console application will generate the following output.

C:\cis218> Welcome to CIS218 Visual C#!

A later section will discuss the differences between a console and a GUI application. In the next section, the instruction will temporarily jump off the topic to explain what the above source code includes and what its statements mean.

It is necessary to note that C# compiler is installed only when a version of Visual Studio (such as Visual Studio Community) is installed to a Windows machine. Without a successful installation of Visual Studio version, the above C# source code cannot be compiled into an .exe application. With the Visual Studio installed in a Windows machine, programmers can launch the Developer Command Prompt (not the regular Window Command Prompt). users need to open the , press the Windows logo key , and then enter dev on the Start menu to bring a list of installed apps, choose the Developer Command Prompt. By default, the Developer Command Prompt displays the following path after being launched, where “X” is the drive name (such as “C”).

X:\Program Files\Microsoft Visual Studio\2017\Community>

Basic structure In the following sample code, “Samples” is a user-declared namespace. In C#, “namespace” is of a C# used to control the scope of class and method names. program namespace Samples { class Welcome

Visual C# - Penn P. Wu, PhD. 3 { static void Main(string[] args) { System.Console.Write("Welcome to CIS218 Visual C#!"); } } }

One namespace can contain multiple classes. However, user-declared namespaces are only useful in larger programming projects. In a small-scale coding project, with or without declaring a namespace does not matter a remarkable difference. The following table compares two C# codes. They produce the same results, yet one has declared namespace while the other does not.

With namespace Samples { class Welcome { static void Main(string[] args) { System.Console.Write("Welcome to CIS218 Visual C#!"); } } }

Without class Welcome { static void Main(string[] args) { System.Console.Write("Welcome to CIS218 Visual C#!"); } }

C# allows programmers to split a class definition. When working on large projects, splitting a class over separate files enables programmers to add codes without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. Another benefit to split a class definition is that multiple programmers to work on it at the same time. By the way, after splitting, each “partial class” is known as a “part”. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

The following example explains how to use the partial keyword modifier to split a class definition. The “Sample” class is split into two “partial classes”.

using System; using System.Windows.Forms;

partial class Sample { static String str = "Hello World!"; }

partial class Sample { static void Main(string[] args) { MessageBox.Show(str); // a message box }

Visual C# - Penn P. Wu, PhD. 4 }

Throughout this course, the focus of instructional contents is to help students build a solid hand- coding foundation using C#; therefore, the instructor frequently chooses not to add user- declared namespaces. Most the future sample codes will not contain user-declared namespaces.

Every self-executable C# program must have one “programmer-defined” class. In the above example, “Welcome” is the programmer-defined class. In C#, such class defines an entity which describes the behavior of its members. The only member in the “Welcome” class is the Main() method. A method of the class is a set of statements to be executed to generate desired results.

Every C# program must have at least one “class” that hosts the Main() method to describe to the C# compiler where the application should start with. The Main() method in the class then serves as the “entry point” of the application. The term “entry point” indicates that all statements in the Main() method are the first statements to be executed when the application is launched.

A complicated C# program may contain several “classes”; however, only one of these classes is allowed to host the Main() method. The one and the only one class that hosts the Main() method is known as the “main class”. Each C# “class” is defined by using the class keyword followed by the class identifier, as demonstrated below in which “ProgramEntry” is the identifier and the class is said to be the “ProgramEntry” class. Inside the “ProgramEntry” class, there is a Main() method.

namespace Samples {

class ProgramEntry {

static void Main(string[] args) { }

}

}

The signature of the Main() method is static void Main(string[] args). The parameter, string[] args, in the Main() method can pass arguments generated by the command-line interface (console) to the Main() method when executing the console application in the command-line interface.

Interestingly, GUI and Windows form applications typically run in the Windows desktop environment which does not generate such command-line arguments. Therefore, the string[] args parament is not required for source codes of GUI and Windows form applications. The signature of them is thus: static void Main(). A later section will discuss this topic in detail.

According to Microsoft’s C# Reference, the Main() method must be declare with the static modifier. In C#, any class member declared a static member belongs to the class itself; therefore, static members cannot be referenced through an instance. A static method means the method can only have one version regardless of its type and cannot be overridden. Basically, it means no change is allowed. While C# programs can have many methods and programmers can place these methods in any order in the source code, the Main() method is always the method that starts the program and it must be set to static.

Visual C# - Penn P. Wu, PhD. 5

static void Main(string[] args)

Again, it is necessary to note that the String[] args parameter is only required for console application. Throughout the course, students will write GUI applications with the following version.

static void Main()

The Main() method in C# can also be declared with public access modifier, as shown below. In this case, “public” specifies that the Main() method can be called from anywhere of the program, “static” means that Main() does not belong to a specific object, “void” means the Main() does not return any value, and args is a collection of string arguments generated by the console command-line interface (e.g. the Command Prompt).

public static void Main(string[] args) { }

All the basic input, processing, and output (the so-called “IPO”) activities can be managed to happen inside the Main() method. In the following example, the instructor uses the Write() method, which is one of the output methods of the Console class of the System namespace in the run-time library, to display a string text on the standard output device (typically the screen). In plain English, the following program displays a message on the prompt. The System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions. The Console class provides standard input, output, and error streams for console applications.

namespace Samples {

class ExampleEntry { static void Main(string[] args) { System.Console.Write("Welcome to CIS218 Visual C#!"); } }

}

The C# language core provides many namespace, such as the System namespace. Each namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions. A “namespace” often consists of several pre-created “classes” of the .NET Framework as the run-time library. These “classese” are created by the programers who created the C# language core. There are two different ways to use namespaces in C#: (a) the use of fully qualified name and (b) the use of “using” directives.

In the following, each time when the WriteLine() method is called, its fully qualified name (System.Console.WriteLine) must be used; therefore, the following example requires programmers to specify the same fully qualified name every time when the WriteLine method is used. By the way, the WriteLine() displays a string text followed by a new line while Write() method display a string without adding a new line.

namespace Samples {

class ExampleEntry

Visual C# - Penn P. Wu, PhD. 6 { static void Main(string[] args) { System.Console.WriteLine("Welcome to CIS218 Visual C#!");

System.Console.WriteLine("You should have fun.");

System.Console.WriteLine("Create your own applications."); } }

}

If the use of fully qualified name is redundant, programmers can choose to import a given namespace with the “using” directive. The following illustrates how to import the System namespace. Every C# statement must end with a semiclon (;), which indicates the end of line.

using System;

With the namesapce being imported, “classes” provided by the namesapce can be referenced and members of these “classes” can be use as a tool. The following statement demonstrates how to use the Write() method of the Console class. By the way, there is a dot (.) operator, known as “member of” operator, between a class identifier and the method.

using System;

namespace Samples {

class ExampleEntry { static void Main(string[] args) { Console.WriteLine("Welcome to CIS218 Visual C#!");

Console.WriteLine("You should have fun.");

Console.WriteLine("Create your own applications."); } }

}

In the above console application code, the Main() method contains a parameter, args, which is an array of the string type. The following example demonstrates how to print out the command line arguments using a foreach loop. A later lecture will discuss the foreach structure in details.

//Filename: ProgramEntry.cs using System;

namespace Samples {

class ExampleEntry {

static void Main(string[] args) { foreach (string s in args) { Console.WriteLine(s);

Visual C# - Penn P. Wu, PhD. 7 } }

}

}

After successful compilation, the following demonstrates how to test the above program.

C:\cis218>ProgramEntry.exe anita catherine lucy anita catherine lucy

Apparently, by importing the System namespace with the “using” directive at the beginning of the program; every member of the System class can be directly called for use without the need to specify the fully qualifiedg name (e.g. System.Console.Write()). Throughout this course, the instructor will adopt this short-hand way to eliminate the use of fully qualified names.

C# programs heavily rely on these input/output methods to take inputs from users and display results of processing as outputs. The Console class also provides the Read() method to read the next character from the standard input stream and the ReadLine() method to read the next line of characters from the standard input stream. The following demonstrates how to implment basic input and output in C# console application.

using System;

namespace Samples {

class EntryProgram { public static void Main(string[] args) { Console.Write("What is your name: ");

String name = Console.ReadLine();

Console.WriteLine("Buenos Dias, " + name + "."); } }

}

In the next section, the instructor will return to the discussion of GUI applications as well as the differences between GUI and console applications. By the way, a later lecture will explain the generic anatomy of C# programs.

C# coding This section is meant to provide students with a basic understanding of three types of C# codes, environment console, GUI (graphical ), and Windows “form”, in order to become familiar and the with the editing tools, compiler, and program-testing environment. compiler A “console” application is a non-GUI application designed to be executed in a command-line interface. However, through this semester, students will focus on creating GUI applications. This lecture is probably the only lecture discussing the building of console applications.

Type Description Example

Visual C# - Penn P. Wu, PhD. 8 Console Applications designed to run in a text- only computer interface, such as the Command Prompt.

GUI An application that displays its result on a simple GUI-based dialog box.

Form A “form” is a rich client application for desktop, laptop, and tablet PCs.

Most textbooks discuss C# programming with examples of console applications. A console application is a designed to be used via a text-only computer interface, such as the Developer Command Prompt which is a command line interface. The following is an example of a simple console application. The file name is “Welcome.cs”.

//File name: Welcome.cs namespace Samples { class Welcome { static void Main(string[] args) { System.Console.Write("Welcome to CIS218 Visual C#!"); } } }

The following is the sample output of the above code.

C:\cis218>sample Welcome to CIS218 Visual C#!

GUI vs. Unlike text-based command-line, every GUI-based (Windows, Mac, and console Linux) provides a unique GUI environment known as the “OS platform”. These OS platforms applications may not be compatible to one another; therefore, each of them usually provides a platform- specific (application programming interfaces). C#, as one of the .NET languages, naturally adopts the .NET Framework APIs.

By narrowing the scope of application development to “input” and “output”, one significant difference between console and GUI applications is the design of the UIs (user interfaces). The following table compares the input and output tools provided by console and GUI APIs.

Type Input Output Console Read(), ReadLine() Write(), WriteLine()

GUI MessageBox.Show() Custom-made input box

The .NET Framework includes a MessageBox class with a Show() method to display a string text in dialog box. A MessageBox object typically contains text, buttons, and symbols to inform and instruct the user what to do. The MessageBox class belongs to the

Visual C# - Penn P. Wu, PhD. 9 System.Windows.Forms namespace which provides classes for creating Windows-based applications that take full advantage of the rich user interface features available in the operating system. The instructor believes the use of dialog boxes is one of the simplest approaches to create basic, simple, and standalone applications for Windows operating systems. Interestingly, the .NET Framework does not provide a simple input dialog box. In a later lecture, the instructor will introduce a custom-made input box code prepared by the instructor for students to use in this course.

The following is a very simple code that creates a GUI-based application. It uses the Show() method of the MessageBox class of the.NET Framework (APIs) to display a string literal, “Hello World”. There is a Main() method inside a programmer-defined class named Hello.

//File name: Hello.cs namespace Samples {

class Hello { static void Main() { System.Windows.Forms.MessageBox.Show("Hello World!"); } }

}

By the way, this sample code also demonstrates how to use “fully qualified name” to call the Show() method. In programming, a fully qualified path name (FQPN) is the full path of a resource, directory or file, stored in a computer. The following technically means “Show() function of MessageBox of Forms of Windows of the System namespace.”

System.Windows.Forms.MessageBox.Show() System  Windows  Forms  MessageBox  Show()

The following is a sample output of the above code. The text is displayed within a GUI dialog box.

While the above example chooses to use the “fully qualified name” to call the Show() method of the MessageBox class: System.Windows.Forms.MessageBox.Show(), programmers can import the System.Windows.Forms namespace with the “using” directive to eliminate the need of using “fully qualified name”, as shown below.

using System.Windows.Forms;

In the following example, the instructor adds an “using” directive to import the System.Windows.Forms namespace; therefore, the Show() method can be called without using the “fully qualified name”. The above code can be re-written to the following.

Visual C# - Penn P. Wu, PhD. 10 //File name: Hello.cs using System.Windows.Forms;

namespace Samples {

class Hello { static void Main() { MessageBox.Show("Hello World!"); } }

}

It is necessary to note that the Main() method does not include any parameter as opposed to Main(string[] args) of console applications. Again, GUI applications do not use command-line interface (as do console applications); therefore, they do not need to collect arguments generated by the command-line interface.

Interestingly, by running the GUI application in a Command Prompt, the args parameter can still pick up command-line arguments if the Main() method may contains the args parameter. Again, the args keyword represents an array of command-line arguments. In the following example, if compiled with csc.exe sample.cs and then launched in the Command Prompt, can pick up two strings. The combined string literals is then display in a message box. The MessageBox class provides the definition to create a that displays messages in a pop-up window.

//File Name: sample.cs using System.Windows.Forms;

namespace Sample { static class Example { static void Main(string[] args) { MessageBox.Show("Welcome, " + args[0] + " " + args[1]); } } }

A sample output looks:

C:\cis218>sample.exe Taylor Swift

The GUI output looks:

Students will use the message box frequently throughout this semester. The next section will introduce the “Windows Forms application. Yet, a later lecture will discuss how to hand-code “Windows Forms” applications in detail.

Visual C# - Penn P. Wu, PhD. 11 Windows Microsoft defines the term “form” as a rectangular area that has a combination of GUI Forms components to present information and/or interact with the users by accepting input from the application user and displaying the results of processing. Most applications run in a Windows operating system are “form-based” application. The following figure is a sample of form-based application.

“Windows Forms”, on the other hand, is the name given to the application programming interfaces (APIs) included as a part of the .NET Framework. C# is an ideal language for creating “Windows form applications”. Throughout this course, students will often write code to inherit the System.Windows.Forms.Form class provided by the .Net Framework to create multiple-control Windows form applications. The term “control” refers to GUI components such as a button, a checkbox, or a drop-down menu. A later lecture will discuss how to create a rich client application in detail.

The bare minimum code to create a generic Windows form is illustrated below. It uses the Form class of the System.Windows.Forms namespace to declare and construct a Windows form object named “form1”. It also uses the Run() method of the Application class to begin running a standard application’s message loop on the current to create the “form1” object.

// File Name: Sample.cs using System; using System.Windows.Forms;

namespace Samples {

class Sample { static int Main() { Form form1 = new Form();

Application.Run(form1);

return 0; } }

}

Programmers can manually type in the above code in a generic (such as Notepad), as shown below, save it as a text file named sample.cs, and then compile it.

Windows form applications support the “Event Model” which can interact with users. A later lecture will discuss “event programming” in detail. The following shows how a Windows application uses its Button control to handle the “Click” event.

Visual C# - Penn P. Wu, PhD. 12 using System; using System.Windows.Forms;

namespace Samples {

class Sample { static int Main() { Form form1 = new Form();

Button button1 = new Button(); button1.Text = "Close"; button1.Location = new System.Drawing.Point(10, 10); button1.Click += new EventHandler(button1_Click); form1.Controls.Add(button1);

Application.Run(form1);

return 0; }

private static void button1_Click(Object sender, EventArgs e) { Application.Exit(); } }

}

The above code creates a simple Windows application that looks similar to the following. By clicking the “Close” button, the application will be destroyed.

void Main() Many students who are new to C# often confused about the difference between void Main() vs. int Main() and int Main(). They also wonder if C# allows the Main() method to return either void or an int? This issue could have a historical background that needs students’ attentions. Older languages, such as C and C++, require the console application’s “main()” method to return an int value to report (or communicate) processing status of the main() method to/with other programs or scripts that invoke the executable file.

Yet, such reporting or communication is not required in C#, particularly the “Windows form applications” written in C#. When a program executes in the Windows environment, any value returned from the Main function is stored in an environment variable called ERRORLEVEL. In the instructor’s opinion, using either void or int type of Main() in C# is just a compromise of programmers’ preferences.

The following compares these two types, void and int. Both programs produce the same result; however, the int type must return an integer to its caller. By the way, traditionally, a return value of zero indicates successful execution. The example on the right is a simple program that returns zero from the Main function. The zero indicates that the program ran successfully.

Visual C# - Penn P. Wu, PhD. 13 void int using System; using System; using System.Windows.Forms; using System.Windows.Forms;

public class Number public class Number { { static void Main() static int Main() { { MessageBox.Show("Hello!"); MessageBox.Show("Hello!"); } } return 0; } }

Apparently, returning void allows for slightly simpler code. Most of the sample codes provided by Microsoft also choose to use void type. The instructor thus chooses to use the void type throughout this course. Throughout this course, students can treat the following as the “template code” which will be seen on most of the sample programs provided in lectures.

using System;

public class Example { public static void Main() {

} }

Practical Along the history, C and C++ are the most popular programming languages while Java gained a Examples good ground for sometimes before Python became a popular language. On the hand, C# language is recently becoming one of the most versatile, powerful, innovative, and evolving programming languages ever. Programmers can use C# to develop enterprise-level , while freelancers can use C# to develop simple and casual application to take advantage of the .Net Framework. For example, the following is a simple C# code that access the .Net Framework’s System.Environment.OSVersion object and displays its VersionString property.

using System; using System.Windows.Forms;

class Samples { static void Main() { OperatingSystem os = System.Environment.OSVersion;

MessageBox.Show(os.VersionString); } }

Without much coding efforts, developers can obtain a large amount of data from a computer by using tools provided by the .Net Framework.

Review 1. Which is a valid file name of a C# code? Questions A. lab1.cs B. lab1.css C. lab1.cpp D. lab1.csharp

Visual C# - Penn P. Wu, PhD. 14 2. Which can declare a namespace named "Narumi" in C#? A. new namespace Narumi { } B. namespace Narumi { } C. using namespace Narumi { } D. Namespace Narumi = new Namespace();

3. Which creates a C# class named “apple”? A. class Apple { } B. new class Apple { } C. public: static class Apple { } D. public: new class Apple { }

4. Which is the correct way to compile a Visual C# code named “lab0.cs” in the command-line interface? A. csc.exe /clr lab0.cs B. csc.exe /t:win lab0.cs C. csc.exe /s:Main lab0.cs D. csc.exe lab0.cs

5. The following is a sample code of __ application.

class Test { static void Main() { System.Console.Write("Welcome to CIS218 Visual C#!"); } }

A. Windows-based GUI B. Windows Forms C. Console D. All of the above

6. Which directive can eliminate the need to use fully qualified name of the following statement?

System.Windows.Forms.MessageBox.Show("Hello World!");

A. using Windows; B. using System.Windows; C. #include System.Windows.Forms; D. using System.Windows.Forms;

7. Given the following code segment, which statement is correct?

static int Main(string[] args) { }

A. args is the parameter of the Main() method. B. args is an individual String literal. C. args is an indicator to eliminate all the command-line arguments. D. args must be declared a String variable, not an array.

8. Given the following code, which statement must be added to actually create the Windows form?

using System.Windows.Forms; public class Example {

Visual C# - Penn P. Wu, PhD. 15 static int Main() { Form form1 = new Form();

return 0; } }

A. Application.Run(new form1); B. Application.Run(new Form); C. Application.Run(form1); D. Application.Run(form);

9. Given the following code, which can display "Hello world" is a pop-up message box?

using System.Windows.Forms; public class Example { static int Main() {

return 0; } }

A. MessageBox->Show("Hello world!"); B. MessageBox::Show("Hello world!"); C. MessageBox.Show("Hello world!"); D. MessageBox Show("Hello world!");

10. The following is an example of ___.

namespace Orange { partial class orange { } partial class orange { } }

A. hybrid namespace B. hybrid class C. partial namespace D. partial class

Visual C# - Penn P. Wu, PhD. 16

Lab #1 Introducing Visual C#

Preparation #1: “Hide extensions for known file types” option.

(For Windows 10/8 Users Only) (For Windows 7/Vista Users Only) 1. Launch the Windows Explorer. 1. Launch the Windows Explorer.

2. Click “View”. 2. Click “Folders and search options”.

3. Check the “File name extension” option. 3. Click the View tab, and then uncheck the “Hide extensions for known file types” option.

4. Proceed to Preparation #2 now.

4. Click OK. Proceed to Preparation #2 now.

Preparation #2: Download Visual C# 2017 Community 1. Use to visit https://www.visualstudio.com/downloads/ to download and install the Visual Studio Community for free. The entire installation procedure might take up to 40 minutes or longer.

Preparation #3: Searching for the Developer Command Prompt Windows 10 .1 1. Open the Start menu, press the Windows logo key 1. Go to the Start screen, press the Windows logo on the keyboard. key on the keyboard.

2. On the Start menu, enter developer. This step will 2. On the Start screen, press CTRL + TAB to open bring a list of installed apps. the Apps list and then enter V. This will bring a list of installed apps.

3. Choose the Developer Command Prompt.

Visual C# - Penn P. Wu, PhD. 17

3. Choose the Developer Command Prompt (for VS2017).

Learning Activity #1: Using Visual C# Command Line Compiler 1. On the keyboard, hold the Windows logo ( ) and press E to launch the Windows Explorer.

2. Right click the “C” drive (or “Local Disk (C:)”), select “New” and then “Folder” to create a new directory named “C:\cis218”.

3. Launch the Developer Command Prompt. Do not use regular Windows Command Prompt.

4. In the prompt, type cd c:\cis218 and press [Enter] to change to the C\cis218 directory. The prompt changes to:

C:\Program Files\Microsoft Visual Studio\2017\Community>cd c:\cis218 C:\cis218>

5. In the prompt, type notepad lab1_1.cs to create a new source file named “lab1_1.cs”, and then click Yes.

C:\cis218>notepad lab1_1.cs

6. Type the following contents into the Notepad as shown in Figure 1-1:

using System; using System.Windows.Forms;

class lab1_1 { static void Main() { MessageBox.Show("Welcome to CIS218 Visual C#!"); }

Visual C# - Penn P. Wu, PhD. 18 } Figure 1-1

7. Click File and then Save to save the file. Click File and then Exit to exit Notepad.

8. Type csc /target:winexe lab1_1.cs and press [Enter] to compile the source file. This creates a new executable console program named lab1_1.exe.

C:\cis218>csc csc /target:winexe lab1_1.cs Microsoft (R) Visual C# Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporation. All rights reserved.

9. Type dir lab* and press [Enter] to verify if the compilation succeeds, which means the compiler creates a self-executable file named “lab1_1.exe”.

Volume in drive C has no label. Volume Serial Number is 30D5-B37B Directory of C:\CIS218 08/25/2016 06:17 PM 104 lab1_1.cs 08/25/2016 06:18 PM 3,072 lab1_1.exe 2 File(s) 3,176 bytes 0 Dir(s) 31,759,298,560 bytes free

10. Type lab1_1.exe to run the console program. The output looks:

11. Download the “assignment template” and rename it to lab1.doc if necessary. Capture a screen shot similar to the above and paste it to the Word document named lab1.doc (or .docx).

Learning Activity #2: console argument 1. Under the C:\cis218 directory, type notepad lab1_2.cs and press [Enter] to create a new source file named “lab1_2.cs”, and then type the following contents.

using System.Windows.Forms;

namespace Sample { static class Example { static void Main(string[] args) { MessageBox.Show("Welcome, " + args[0] + " " + args[1]); } } }

2. Type csc /target:winexe lab1_2.cs and press [Enter] to compile.

3. Type lab1_2.exe Taylor Swift and press [Enter] to test the program.

Visual C# - Penn P. Wu, PhD. 19

4. Capture a screen shot similar to the above and paste it to the Word document named lab1.doc (or .docx).

Learning Activity #3: Partial class 1. Under the C:\cis218 directory, type notepad lab1_3.cs and press [Enter] to create a new source file named “lab1_3.cs”, and then type the following contents.

using System; using System.Windows.Forms;

partial class Sample { static String str = "Hello World!"; }

partial class Sample { static void Main(string[] args) { MessageBox.Show(str); // a message box } }

2. Type csc /target:winexe lab1_3.cs and press [Enter] to compile.

3. Type lab1_3.exe and press [Enter] to test the program.

4. Capture a screen shot similar to the above and paste it to the Word document named lab1.doc (or .docx).

Learning Activity #4: A Generic Windows form 1. Under the C:\cis218 directory, type notepad lab1_4.cs and press [Enter] to create a new source file, and then add the following contents.

using System; using System.Windows.Forms;

namespace Samples {

class Sample { static int Main() { Form form1 = new Form();

Application.Run(form1);

return 0;

Visual C# - Penn P. Wu, PhD. 20 } }

}

2. Type csc /target:winexe lab1_4.cs and press [Enter] to compile.

3. Type lab1_4.exe and press [Enter] to test the program.

4. Capture a screen shot similar to the above and paste it to the Word document named lab1.doc (or .docx).

Learning Activity #5: 1. Under the C:\cis218 directory, type notepad lab1_5.cs and press [Enter] to create a new source file, and then add the following contents.

using System; using System.Windows.Forms;

class Samples { static void Main() { OperatingSystem os = System.Environment.OSVersion;

MessageBox.Show(os.VersionString); } }

2. Type csc /target:winexe lab1_5.cs and press [Enter] to compile.

3. Type lab1_5.exe and press [Enter] to test the program.

4. Capture a screen shot similar to the above and paste it to the Word document named lab1.doc (or .docx).

Submittal 1. Complete all the 5 learning activities. Create a .zip file named lab1.zip containing ONLY the following self- executable files. (See Appendix C for instructions) • lab1_1.exe • lab1_2.exe • lab1_3.exe • lab1_4.exe • lab1_5.exe • lab1.doc (or .docx) [You may be given zero point if this Word document is missing]

Visual C# - Penn P. Wu, PhD. 21 2. Log in to course site, and enter the course site.

3. Upload the zipped file to Question 11 of Assignment as response.

Programming Exercise: 1. Use Notepad to create a new file named ex01.cs with the following heading lines (be sure to replace YourFullNameHere with the correct one):

//File Name: ex01.cs //Programmer: YourFullNameHere

2. Under the above two heading lines, write C# codes to display a text “YourFullNameHere wrote this code!” in a message box, as shown below. [Hint: read one of the sample codes in the lecture note and learning activity #1.]

Must be your full name

3. Download the “programming exercise template” and rename it to ex01.doc. Capture a screen shot similar to the above figure and then paste it to the Word document named “ex01.doc” (or .docx).

4. Compress the source code (ex01.cs), the executable (ex01.exe), and the Word document (ex01.doc or .docx) to a .zip file named “ex01.zip”.

Grading Criteria: • You must be the sole author of the codes. • You must meet all the requirements in order to earn credits. • You must submit both source code (ex01.cs) and the executable (ex01.exe) as response to Question 12 of Assignment 01 to earn credit. • No partial credit is given.

Appendix A: Compress files to .zip format 1. Open the Windows Explorer and change to the directory that has all the following files (e.g. C:\cis218).

lab1_1.exe, lab1_2.exe, lab1_3.exe, lab1_4.exe, and lab1_5.exe

2. Highlight all the five files, and then right click the highlighted area as shown below:

Visual C# - Penn P. Wu, PhD. 22 3. Select “Send to”, and then “Compressed (zipped) folder”. This step will create a .zip file.

4. Right click the lab1_x.zip (while x is a number) file and then select “Rename”.

5. Change the file name to “lab1.zip”.

Visual C# - Penn P. Wu, PhD. 23