a C# learning space
|
Buy me a cup of coffee?
|
C# Basics
This is an extremely brief discussion of the C# language and programming in general. I like the learn by doing method and so my hope is that most of the
questions you might have will be answered by typing the code and using the coding interface, VisualStudio 2017 in this case.
C# is a case sensitive language. It sees strVariable and strvariable as two different variables even though they are spelled the same. This is why most
programmers have a method for naming their variables. I like to use the first method, which is often referred to as camel case. Find the method you like
and stick with it.
Sequence
Writing any code in a sequence simply means that each line of code will execute sequentially; one after the other. Each line in C# ends with a semicolon (;).
Condition
Also known as branching. Here a condition is tested to be either true or false. If it is true the following statements are executed otherwise they are not.
The syntax for a if/then condition is if ([condition]) { [code to execute if true]; }. Your condition to evaluate is inside paranthesis and the code to execute
if true is inside the curly braces.
Typically you would see something like:
if (int x == 1)
{
string strResult = true;
}
A typical mistake people make (I do it myself) is in the condition statement you see the equals sign is doubled. This means "equal to". As opposed to the
single equals sign, which is an assignment opperator. And you see that in the code that executes when true, we assign the value of "true" to the string
variable, strResult.
Also, you notice that the data type of the variables x and strResult are defined right before their use. This is only done the first time you are using that
variable. After that you can just type the variable name. If either of these variables were being used outside of the condition code block you would need to
define the datatype there so that the variable is within scope of all the code where it is being used.
You can expand on the condition code by using the "else" clause. This way there is code being executed whether the condition evaluates to true or false.
Rather than show all the variations of the condition, you will have ample oportunity to see it in action in the Sudoku Sharp code that we write. But note that
there is another type of condition code you can use and that is the "switch", which executes a batch of code based on a variable. So, you could have a variable
with the possible values of 1, 2, or 3 and when the condition is evaluated the set of code for 1, 2, or 3 executes.
Looping
Also known as reiteration. A set of code executes a set number of times or while a condition evaluates to true. This is done with a "for loop" for a set
number of loops and a "while loop" to loop while a condition is true or until the condition changes.
A sample of a "for loop" (and one we use quite often in the Sudoku Sharp code) would look like:
for (int x = 0; x <= 80; x++)
{
cell[x].CellValue = 0;
}
Here we are assigning (single equal sign) the value 0 to each of the 81 cells in the Sudoku puzzle.
First we initialize the variable "x" to 0. Then we evaluate "x" and as long as it is less than 81, the code inside the curly braces gets executed. Then we
increment "x" by 1, test again and execute until "x" is 81.
Object Oriented Programming (OOP)
C# is an object oriented programming language. OOP is a way to create a type of an object known as a class, and then reuse that object over and over again.
You will see in the Sudoku Sharp program that I define a "grid" object with properties and methods that help initiate and manipulate the Sudoku grid. I will
use the same method for defining what a "cell" is within the Sudoku grid.