Lecture #1 Introducing Visual C# Introduction to According to Microsoft, 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 system called the common language runtime (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 “assembly” 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 text file 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\Microsoft Visual Studio\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). Windows 10 users need to open the Start menu, 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.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages23 Page
-
File Size-