C-Family Programming - College Homework Help and Online Tutoring
It is difficult for a developer to consider writing C# code to any real degree without dipping into the .NET Framework or Mono extensively. As a result, most tutorials and examples you encounter take this coupling for granted and do not really differentiate between the two, and this will be no exception. The following code is for use with .NET 4.6 (the latest version of the framework at the time of writing) but should work on Mono or older versions without too many problems. First let’s turn our attention to the inevitable “Hello World” application. First off we can make a new solution (this code is targeted at Visual Studio, the dominant IDE for C#), selecting the Console Application template. This gives us a single code file, “Program.cs”, and an application config file called App.config, which we will disregard for now. By default, the following code is given to us: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { class Program { static void Main(string[] args) { } } } Anyone familiar with writing C console applications will recognize what they are seeing here. The program class is designed to be the startup class for our application (from the project and solution properties) and the Main() method is our entry point. At execution time, code will come in here, execute to the end of Main() function, and then quit. If you run this application by hitting ‘start’, you will see a console window appear and disappear just as quickly, which means our empty application has executed without error. The first thing we can do is add the following line to our application so that when it has completed, the application waits for us to press a key before exiting. We will leave this line at the end of our application for the duration of this tutorial. Add it, and try running your application again. Console.ReadKey(); Now add the following line above this one, to create our ‘Hello World’ application. Console.WriteLine("Hello, World!"); When you run this, you will now see your greeting as text. Let’s examine what’s happening here with both lines of code. Console is a static class provided to us from the System namespace, which we can see at the top of our file (“using System;”). Static classes do not require an instantiated object to execute their functions, and are instead executed solely from any code module that has access to the namespace of your static class. The static methods we are using here are ‘WriteLine’ and ‘ReadKey’, which are fairly self-explanatory. WriteLine is passed one argument, which is our text string, “Hello World!” When we run that line of code, the application prints our text to the console, followed by a new-line. The program then waits (also referred to as ‘blocking for input’) at the ‘Console.ReadKey()’ line until the user pushes a key and the program can continue to the end, exiting. As you can see, these two lines actually perform a large number of low-level operations without us ever having to think about them, which is the strength of high level languages from a programming perspective. Let’s go ahead and customize our input by reading from the console. Above our WriteLine method, we can add another method – ReadLine. ReadLine is like ReadKey, but instead we wait for a user to type several characters and press return, at which point the text they have typed will be returned and we can store it in a variable. This is done with the equals (=) operator. In the following example, we combine variable declaration and assignment into one line after prompting for input. The output of the ReadLine method is subsequently stored in the userName variable which is of type string (as in a sequence of characters, or text). Now, our program looks like this: static void Main(string[] args) { Console.WriteLine("Please enter your name:"); string userName = Console.ReadLine(); Console.WriteLine("Hello, " + userName); Console.ReadKey(); }
However, this program is identical to the following:
static void Main(string[] args) { Console.WriteLine("Please enter your name:"); string userName; userName = Console.ReadLine(); Console.WriteLine("Hello, " + userName); Console.ReadKey(); } This is because declaring a variable and later assigning it is the same as doing both actions on the same line. In fact, the second example is a bad practice, as you may forget to assign your variable to some value before you use it, which will cause your program to crash. The other part of this example that has changed is the ‘WriteLine’ method, which now is outputting "Hello, " + username. In this instance, the + operation usually associated with adding numbers is combining two strings. As a result, when executed, the value inside ‘username’ is appended to the end of “Hello, “ to create a new string. Try running this program now and experimenting with the output. One brief word on variable declarations before we move on: You may see other tutorials and subsequent ones in this series use ‘var’ instead of ‘string’ (or any other type like ‘int’). This is because modern versions of C# can save the programmer the job of declaring a variable type and instead let the compiler do that work. As a result, the compiler has access to information about the ‘Console.ReadLine()’ method and can correctly determine that it will return a string, which it substitutes for ‘var’ at compile time. As a result, this is equally valid, and an even better practice: var userName = Console.ReadLine(); Having built this basic application you have gained a brief insight into input/output methods and the structure of an application. Once you understand such procedural code and basic variables, it is time to move on to understand object oriented programming in order to make the most of the power of a high level language like C#. Since we have tutors in all C-Family Programming related topics, we can provide a range of different services. Our online C-Family Programming tutors will: With these capabilities, our college C-Family Programming tutors will give you the tools you need to gain a comprehensive knowledge of C-Family Programming you can use in future courses. Our tutors are just as dedicated to your success in class as you are, so they are available around the clock to assist you with questions, homework, exam preparation and any C-Family Programming related assignments you need extra help completing. In addition to gaining access to highly qualified tutors, you'll also strengthen your confidence level in the classroom when you work with us. This newfound confidence will allow you to apply your C-Family Programming knowledge in future courses and keep your education progressing smoothly. Because our college C-Family Programming tutors are fully remote, seeking their help is easy. Rather than spend valuable time trying to find a local C-Family Programming tutor you can trust, just call on our tutors whenever you need them without any conflicting schedules getting in the way.