Sept-Dec, Introduction to C#
|
Page 35
|
|
|
A variable stores a value during the running of a program
|
|
|
Type
|
Description
|
|
string
|
contains alpha numeric characters, persons name
|
|
int
|
a number value without decimal points, persons age
|
|
double
|
a number with decimal points, an amount of money
|
|
boolean
|
either true or false
|
|
Page 45
|
|
|
Examples
Console.WriteLine("Hello World"); Console.Write("and Hello Moon"); |
|
|
Line
|
Description
|
|
1
|
insert a carriage return and leave cursor on next line
|
|
2
|
leave cursor on same line
|
|
Page 49
|
|
|
$"{varName1} {varName2}"
|
|
|
Examples
Console.WriteLine(${"Name : {studentName}"); Console.WriteLine($"GPA : {studentGpa}"); |
|
|
Line
|
Description
|
|
1
|
{ and } enclose the variable name to display
|
|
Page 62
|
|
|
Operand
|
Description
|
|
A==B
|
checks two operands to see if equal. false
|
|
A!=B
|
check operands to see if not equal. true
|
|
A<B
|
is A less than B? false
|
|
A>=B
|
is A greater than or equal to B. true
|
|
A<=B
|
is A less than or equal to B? false
|
|
Pages 79-83,86-87
|
|
Page 93
|
|
|
[static] [public|private] return-type MethodName ([param-list]}
|
|
|
Type
|
Description
|
|
[static]
|
no need to create instance, call directly
|
|
[public | private]
|
can only be called from within this class
|
|
[void | int | double | string]
|
return type, void infers nothing returned
|
|
MethodName
|
use PascalCase for naming the method
|
|
([param-list])
|
specify paramater type, separate with commas
|
|
Tip
|
Reason
|
|
Comment your code
|
allows you or someone else to more easily understand the code now or in the future
|
|
Watch your variables and constant naming convention
|
Use camelCase for ordinary variables, and UPPERCASE for constants - it's easy then to tell them apart
|
|
Pages 43-45
|
|
|
[type] <varName> = <value>;
|
|
|
Examples
int studentAge=19; string studentName="Walter"; double studentGpa=78.68; boolean studentRegistered; |
|
|
Item
|
Description
|
|
type
|
common types int, string, double, boolean
|
|
<varName>
|
the name of variable in which we store value
|
|
Page 54
|
|
|
Example
studentName=Console.ReadLine(); |
|
|
Line
|
Description
|
|
1
|
read input from keyboard, assign to studentName
|
|
Page 58-59
|
||
|
$"{x:c} {y:p} {z:n3}"
|
||
|
Code
|
Format
|
Output
|
|
C or c
|
currency
|
€1,245.44
|
|
P or p
|
percent
|
4.00%
|
|
N or n
|
Number
|
103,423.346
|
|
Page 64-65
|
|
Page 84
|
|
Page 92
|
|
|
Item
|
Description
|
|
return type
|
a method can return a value - of type int, double, string. if nothing returned, then return-type is void
|
|
sharing data between methods
|
parameters - values passed to a method call. also known as arguments.
class level variables - available to all methods, scope is global |
|
calling a method
|
We must call the method to invoke it.
|
|
predefined methods
|
Includes Console.WriteLine("Hello) - has parameters
Console.Readline() - no parameters |
|
Tip
|
Reason
|
|
Use code indentation
|
When using conditional (If), Loops (While, Do While, For) and Methods - indent your code. Make it easier to read for everyone.
|
|
Follow the recipe
|
Make your input and output actually look like what is presented on the CA question.
|
|
Pages 39,53,93,30
|
|
|
Type
|
Description
|
|
variables
|
camelCase, first letter is lowercase, other words first letter uppercase
|
|
constants
|
use uppercase, e.g. VATRATE
|
|
methods
|
PascalCase, first letter of each word is uppercase
|
|
comment our code
|
// what does this code do?
/* reminds colleagues, our future selves */ |
|
Page 55
|
|
|
Example
studentAge = int.Parse(Console.ReadLine()); studentGrade = double.Parse(Console.ReadLine()); |
|
|
Line
|
Description
|
|
1
|
Integer value is returned and assigned
|
|
2
|
Double value may contain decimals
|
|
Page 57
|
|
|
$"<text>{<expression>,<field-width>}<text>..."
|
|
|
Examples
|
|
|
$"Name :{studentName,20}"
|
|
|
$"{"Name",-20} :{studentName}"
|
|
|
Line
|
Description
|
|
1
|
right justify, 20 leading spaces before student name
|
|
2
|
left justify, 20 spaces after label "Name"
|
|
Pages 66-73
|
|
Pages 85,87
|
class Program
{
static void Main(string[] args)
{
static string salutation="Hello";
string name=GetName();
PrintGreeting(name);
}
static private string GetName()
{
Console.WriteLine("Enter First Name : ");
string firstName=nameConsole.ReadLine();
return firstName;
}
static private void PrintGreeting(string name)
{
Console.WriteLine($"{salutation} {name}!");
}
}} // end class
|
Console.OutputEncoding=System.Text.Encoding.UTF8; // display special symbols like currency
|
|
Carriage Return or "\n" // A carriage return moves the cursor onto the next line in our console display.
|
|
Lab worksheet is a solution. Each question is a project
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.