C# is a curly, compiled, statically-typed, semi-colon-using, object oriented programming language developed by Microsoft as part of their .NET family of languages. This means that C# shares a library of code with Visual Basic, another .NET language, so the two can easily be combined in larger projects.
C# is named after C++ (if you stick two +'s together you get a #, people who name languages are very funny people). However, C# is a heavily Object Oriented language like Java, and has more in common with Java than C++ because of that focus.
If you visit the online compiler Rextester, you'll notice that their default C# example is this:
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
}
}
}
The first four lines of this program (after the top comment) are using
lines. These tell the compiler what packages from the library to load before we get started. I don't know why RexTester chose these four packages as the default: If you delete the second, third, and fourth packages from this list the "Hello World" program will still work just fine. That first line, using System;
, is what allows us to use Console.WriteLine
later.
Next, you'll see the namespace
line and accompanying curly braces. The namespace block designates everything inside it as part of the same "package." Inside that, we have the public class
named Program
, which will hold our main method, public static void Main
, which is where the program begins, like in most compiled languages.
For comparison, here is RexTester's default Java program, which you'll notice is almost identical:
//'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_111
import java.util.*;
import java.lang.*;
class Rextester
{
public static void main(String args[])
{
System.out.println("Hello, World!");
}
}
To compile C#, you can use RexTester for only the most simple of programs (text output only, no keyboard input). We'll be using Visual Studio, which you can download for free here, since that is the preferred way to work with C#. If you do not have Visual Studio already, begin installing it now, since it takes a long time for most people. While you are installing Visual Studio, be sure to select the "NET Core cross-platform development" workload option, as this will allow you to program in C#.
C# has similar variable types to Java, Visual Basic, and C++. The common types you'll see are:
int myWholeNumber = 123;
double myRealNumber = -1.23;
string myCatsName = "Spencer Reid";
bool myTrueFalseValue = false;
C# also uses the common math operators:
int answer = X + Y - Z*3;
double volume = 4/3 * Math.PI * radius*radius*radius;
// every year on your birthday run this line of code
age += 1;
// this would also be nice
salary *= 2;
Like some languages, but not all (like Python), C# lets you concatenate non-string values with string values:
int score = 100;
string name = "Jane Doe";
Console.WriteLine("Great work " + name +
", you scored a " + score +
"on the exam!");
This means that C# has the "string trick" from JavaScript where you can easily turn any value into a string:
string myString = "" + anyVariableIWant;
To convert between other types, though, C# has a handy Convert
tool:
int myWholeNumber = Convert.ToInt32(myString);
double myRealNumber = Convert.ToDouble(myString);
bool myBoolean = Convert.ToBoolean(myString);
Finally, C# uses the standard rules for naming variables, with a few conventions:
CamelCasing
or under_scores
to aid in readabilityUpperCamelCase
by conventionMath.PI
) use ALL_CAPS_WITH_UNDERSCORES
.L
as a variable name or an O
, whether upper or lower, because these look too much like 1
and 0
.(If you are having trouble with Visual Studio or getting it installed, shoot me an email describing the problem, what you have tried, and screenshots. Then, while you wait for a reply, continue to try to resolve the problem on your own.)
Once Visual Studio is installed and working, follow along with this video: Creating Your First WPF Application.
Although the video is for a previous version of Visual Studio, the steps should be approximately the same:
File > New > Project > C# > WPF Application
Ok
Ctrl+F5
Doing this, you should be able to create the interface for a program. This means that you can have textboxes, buttons, pictures, and so on, but nothing will happen when the user interacts (clicks, etc.) with your program.
This is perfectly alright, it turns out, since C# was written to make creating interfaces and creating "business logic" two separate processes. The benefits to this are:
We'll start with GUIs because:
To submit, write your responses to the following questions in a Word document, then upload it to Blackboard under this week's folder in Current Assignments.
Labs are due two weeks from when they are assigned, on Sunday at 11:59pm eastern time.
Labs are each worth 5% of your final grade. Scoring on your submission will be based on the following rubric:
0% - Student does not submit on time or submits plagiarized or unacceptable work. Double check that you have attached the right file, as usually students get zeros because they upload a previous week's Lab by accident.
1% - Student answers less than half of the questions with sufficient or accurate responses. Make sure that you are leaving yourself enough time to complete the Lab each week, as usually students submit incomplete work because they were rushed at the last minute.
3% - Student answers almost all questions with sufficient and accurate responses. If you encounter a problem or have a question about one of the questions, be sure to post in Ask the Instructor well before 24 hours before the due date, then continue to attempt to resolve the issue on your own while you wait for a reply.
5% - Great job, maximum points! The student answers all questions accurately and sufficiently, demonstrating the best of their ability.
Note, because Labs span two weeks' worth of reading, it is recommended to go through the Lab twice, once after the first reading where you answer everything that you can, then again after the second reading where you answer everything else.
________
.______
literal.Console.WriteLine
, (b) Console.PrintLine
, (c) Console.DisplayLine
, or (d) Console.PutsLine
.per cent increase
, (b) annualReview
, (c) HTML
, (d) alternativetaxcredit
/* like this */
is a (a) line comment or (b) block comment?%
math operator do? For example, 4 % 2
is equal to 0
./
math operator, when used on integers, always rounds down.____
and _____
._______
.22 / 7
in C#? (Hint: are we using integers or doubles in this equation?)22.0 / 7.0
in C#?17 / 2 + 14 / 2
in C#?last name
, (b) phone#
, (c) 123DruryLane
, and (d) air-plane
. Explain what is wrong with each variable name and propose a new, correct name for each.