Getting Started with C# – C# Station. This lesson will get you started with C# by introducing a few very simple programs. Here are the objectives of this lesson: Understand the basic structure of a C# program. Obtain a basic familiarization of what a “Namespace” is. This section covers the list of topics for C# programming examples. These C# examples cover a wide range of programming areas in Computer Science. C# classes - basic example . This is basically my first attempt to understand the classes in C#. I've went through several. C# - Basic Syntax - Learn C Sharp Programming in simple and easy steps starting from Environment setup, Basic Syntax, Data Types, Type Conversion, Variables. This topic links you to sample application and code example topics for Visual Basic and Visual C# programmers. In This Section Visual Basic Code Example Topics. This lesson will get you started with C# by introducing a few very simple programs. Here are the objectives of this lesson: Understand the basic structure of a C#. These pages collect example tutorials, programs and ActiveX controls displaying particular programming techniques in Visual Basic. Visual Basic.NET; Beginners PHP; C#.NET < < Java for Beginners. Visual C#.NET Contents Page. This Home and Learn computer course is an. Obtain a basic understanding of what a Class is. Learn what a Main method does. Learn how to obtain command- line input. Learn about console input/output (I/O). This article contains a collection of C# programs that I wrote when I wanted to learn C# and.NET. I call these types of programs 'Exploratory Programming. 101 Visual Basic and C# Code. This download includes a master set of Visual Basic and Visual C# code samples demonstrating various. What Visual Basic is not H Visual Basic is not, a powerful programming language that enables you to do anything you want. H Visual Basic is not, elegant or fast. A Simple C# Program. There are basic elements that all C# executable programs have and that’s what we’ll concentrate on for this first lesson, starting off with a simple C# program. After reviewing the code in Listing 1- 1, I’ll explain the basic concepts that will follow for all C# programs we will write throughout this tutorial. Please see Listing 1- 1 to view this first program. Warning: C# is case- sensitive. Listing 1- 1. A Simple Welcome Program: Welcome. Namespace Declaration. Program start class. Welcome. CSS. . It can be compiled with the following command line: csc. Welcome. cs. This produces a file named Welcome. Other programs can be compiled similarly by substituting their file name instead of Welcome. For more help about command line options, type “csc - help” on the command line. The file name and the class name can be totally different. Note for VS. NET Users: The screen will run and close quickly when launching this program from Visual Studio . NET. To prevent this, add the following code as the last line in the Main method: // keep screen from going away. VS. NET. Console. Read. Line(); Note: The command- line is a window that allows you to run commands and programs by typing the text in manually. It is often refered to as the DOS prompt, which was the operating system people used years ago, before Windows. The . NET Framework SDK, which is free, uses mostly command line tools. Therefore, I wrote this tutorial so that anyone would be able to use it. Do a search through Windows Explorer for “csc. C# compiler. When you know its location, add that location to your Windows path. Then open the command window by going to the Windows Start menu, selecting Run, and typing cmd. This blog post might be helpful: How to set the path in Windows 7. The first thing you should be aware of is that C# is case- sensitive. The word “Main” is not the same as its lower case spelling, “main”. They are different identifiers. If you are coming from a language that is not case sensitive, this will trip you up several times until you become accustomed to it. The namespace declaration, using System; , indicates that you are referencing the System namespace. Namespaces contain groups of code that can be called upon by C# programs. With the using System; declaration, you are telling your program that it can reference the code in the System namespace without pre- pending the word System to every reference. I’ll discuss this in more detail in Lesson 0. Namespaces, which is dedicated specifically to namespaces. The class declaration, class Welcome. CSS, contains the data and method definitions that your program uses to execute. A class is one of a few different types of elements your program can use to describe objects, such as structs, interfaces , delegates, and enums, which will be discussed in more detail in Lesson 1. Structs, Lesson 1. Interfaces, Lesson 1. Delegates, and Lesson 1. Enums, respectively. This particular class has no data, but it does have one method. This method defines the behavior of this class (or what it is capable of doing). I’ll discuss classes more in Lesson 0. Introduction to Classes. We’ll be covering a lot of information about classes throughout this tutorial. The one method within the Welcome. CSSclass tells what this class will do when executed. The method name, Main, is reserved for the starting point of a program. Main is often called the “entry point” and if you ever receive a compiler error message saying that it can’t find the entry point, it means that you tried to compile an executable program without a Main method. A static modifier precedes the word Main, meaning that this method works in this specific class only, rather than an instance of the class. This is necessary, because when a program begins, no object instances exist. I’ll tell you more about classes, objects, and instances in. Lesson 0. 7: Introduction to Classes. Every method must have a return type. In this case it is void, which means that Main does not return a value. Every method also has a parameter list following its name with zero or more parameters between parenthesis. For simplicity, we did not add parameters to Main. Later in this lesson you’ll see what type of parameter the Main method can have. You’ll learn more about methods in Lesson 0. Methods. The Main method specifies its behavior with the Console. Write. Line(. Console is a class in the System namespace. Write. Line(. We use the “.”, dot, operator to separate subordinate program elements. Note that we could also write this statement as System. Console. Write. Line(. This follows the pattern “namespace. Had we left out the using System declaration at the top of the program, it would have been mandatory for us to use the fully qualified form System. Console. Write. Line(. This statement is what causes the string, “Welcome to the C# Station Tutorial!” to print on the console screen. Observe that comments are marked with “//”. These are single line comments, meaning that they are valid until the end- of- line. If you wish to span multiple lines with a comment, begin with “/*” and end with “*/”. Everything in between is part of the comment. Comments are ignored when your program compiles. They are there to document what your program does in plain English (or the native language you speak with every day). All statements end with a “; ”, semi- colon. Classes and methods begin with “. Any statements within and including “. Blocks define scope (or lifetime and visibility) of program elements. Accepting Command- Line Input. In the previous example, you simply ran the program and it produced output. However, many programs are written to accept command- line input. This makes it easier to write automated scripts that can invoke your program and pass information to it. If you look at many of the programs, including Windows OS utilities, that you use everyday; most of them have some type of command- line interface. For example, if you type Notepad. My. File. txt (assuming the file exists), then the Notepad program will open your My. File. txt file so you can begin editing it. You can make your programs accept command- line input also, as shown in Listing 1- 2, which shows a program that accepts a name from the command line and writes it to the console. Danger! Regardless of the fact that I documented the proper use of command- line arguments before and after Listing 1- 2, some people still send me email to complain that they get an error or tell me there’s a bug in my program. In fact, I get more email on this one subject than any other in the whole tutorial. Please read the instructions to include the command- line argument. For example, type the name of the program, followed by your name: Named. Welcome Your. Name. This is the purpose of Listing 1- 2 – to show you how to handle command- line input. Therefore, you must provide an argument on the command- line for the program to work. If you are running Visual Studio, right- click on the project in Solution Explorer, select Properties, click the Debug tab, locate Start Options, and type. Your. Name into Command line arguments. If you forget to to enter Your. Name on the command- line or enter it into the project properties, as I just explained, you will receive an exception that says “Index was outside the bounds of the array.” To keep the program simple and concentrate only on the subject of handling command- line input, I didn’t add exception handling. Besides, I haven’t taught you how to add exception handling to your program yet – but I will. In Lesson 1. 5: Introduction to Exception Handling, you’ll learn more about exceptions and how to handle them properly. Listing 1- 2. Getting Command- Line Input: Named. Welcome. cs// Namespace Declaration. Program start class. Named. Welcome. . If you don’t, your program will crash. I’ll show you in. Lesson 1. 5: Introduction to Exception Handling how to detect and avoid such error conditions. If you are using an IDE, like Visual Studio, see your IDE’s help documentation on how to set the command- line option via project properties. The actual step can/will differ between IDE’s and versions, so please consult your IDE documentation for more information. In Listing 1- 2, you’ll notice an entry in the Main method’s parameter list. The parameter name is args, which you’ll use to refer to the parameter later in your program. The string type holds characters. These characters could form a single word, or multiple words. Therefore, the type of the args parameter, is a list of words from the command- line. Anytime you add string. By readingargs, you have access to all arguments, minus the application name, that were typed on the command- line. You’ll also notice an additional Console. Write. Line(. The argument list within this statement is different than before. It has a formatted string with a “. The first parameter in a formatted string begins at number 0, the second is 1, and so on. Hold that thought, and now we’ll look at the next argument following the end quote. The args. The first element of an Array is number 0, the second is number 1, and so on. For example, if I typed Named. Welcome Joe on the command- line, the value of args. This is a little tricky because you know that you typed Named. Welcome. exe on the command- line, but C# doesn’t include the executable application name in the args list – only the first parameter after the executable application. Returning to the embedded “. When this command is executed, the value of args. Upon execution of the command- line with “Named. Welcome Joe”, the output will be as follows: Hello, Joe! C# - Program Structure. Before we study basic building blocks of the C# programming language, let us look at a bare minimum C# program structure so that we can take it as a reference in upcoming chapters. Creating Hello World Program. A C# program consists of the following parts: Namespace declaration. A class. Class methods. Class attributes. A Main method. Statements and Expressions. Comments. Let us look at a simple code that prints the words . A program generally has multiple using statements. The next line has the namespace declaration. A namespace is a collection of classes. The Hello. World. Application namespace contains the class Hello. World. The next line has a class declaration, the class Hello. World contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the Hello. World class has only one method Main. The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed. The next line /*..*/ is ignored by the compiler and it is put to add comments in the program. The Main method specifies its behavior with the statement Console. Write. Line(. This statement causes the message . This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio . NET. It is worth to note the following points: C# is case sensitive. All statements and expression must end with a semicolon (; ). The program execution starts at the Main method. Unlike Java, program file name could be different from the class name. Compiling and Executing the Program. If you are using Visual Studio. Net for compiling and executing C# programs, take the following steps: Start Visual Studio. On the menu bar, choose File - > New - > Project. Choose Visual C# from templates, and then choose Windows. Choose Console Application. Specify a name for your project and click OK button. This creates a new project in Solution Explorer. Write code in the Code Editor. Click the Run button or press F5 key to execute the project. A Command Prompt window appears that contains the line Hello World. You can compile a C# program by using the command- line instead of the Visual Studio IDE: Open a text editor and add the above- mentioned code. Save the file as helloworld. Open the command prompt tool and go to the directory where you saved the file. Type csc helloworld. If there are no errors in your code, the command prompt takes you to the next line and generates helloworld. Type helloworld to execute your program. You can see the output Hello World printed on the screen.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |