Syntax, Output, Comments, Variables, Data Types, Type Casting, User Input, Operators Flashcards
Write a “Hello World!” Program that outputs “Hello World!”.
using System;
namespace HelloWorld {
class Program {
static void main(string[] args) {
Console.WriteLine(“Hello World!”);
}
}
}
You have the following format of C# code:
using System;
namespace HelloWorld {
class Program {
static void main(string[] args) {
Console.WriteLine(“Hello World!”);
}
}
}
1. Explain line 1
2. Explain line 2
3. Explain line 3
4. Explain line 4
5. Explain line 5
- using System means that we can use classes from the System namespace. Tells the compiler that the program is using the System namespace, which includes fundamental classes necessary for basic operations, like input/output.
- namespace is used to organize the code, and it is a container for classes and other namespaces.
- class is a container for data and methods, which brings functionality to our programs.
- This is the main entry point of any C# console application. The Main method must be static, and it typicaly returns void (though it can also return int (return 0;)). string[] args represents an array of command line arguments that can be passed to the program when it is executed.
- Console is a class of the System namespace, which has a WirteLine() ethod that is used to output/print text.
Name 2 ways to output text in C#.
WriteLine() and Write() methods.
Describe the WriteLine() Method and Write() Method.
WriteLine() method outputs/prints text and adds a new line for each method.
Write() method does not inserts a new line at the end of the output.
What are comments used for?
In C#, comments are used to add explanatory notes or descriptions within the code. These comments are ignored by the compiler and don’t affect the execution of the program. They help make the code more readable, understandable, and maintainable, especially for others (or yourself in the future) who may be working with the code.
What are the different ways of using a comments?
Single comments (//)
MultiLine comments (/* */
Declare a variable of integer data type, named num and assign a value of 5.
int num = 5;
A variable must be identified with unique names.
The unique names are called identifiers.
What are the general rules to naming a variable?
- Names can contain letters, digits and the underscore character (_)
- Names must begin with a letter or underscore
- Names should start with a lowercase letter, and cannot contain whitespace
- Names are case-sensitive (“myVar” and “myvar” are different variables)
- Reserved words (like C# keywords, such as int or double) cannot be used as names
What are the usage of constant variables?
Can NOT overwrite this variable existing value. This means the variable is unchangeable and read-only.
Declare a constants variable and assign a value to it.
const int num = 3;
What are ways to display a variable?
int number = 35;
Console.WriteLine(number);
Console.WriteLine(“Here is the number:” + number);
Console.WriteLine($”Here is the number: {number}”);
How would you declare multiple variables?
int x = 5, y = 6;
or
int a, b;
a = b = 35;
Describe the meaning of a data type and its importance.
Data type specifies the size and type of a variable values.
It is important to use the correct data type for the corresponding variable; to avoid errors, to save time and memory, but it will also make your code more maintainable and readable.
Data Types can be categorized into 2 categories, what are they?
Value Type and Reference Type.
Describe value type data types.
Store actual data directly & typically stored in the stack like int, float, double, char, bool, decimal, struct, enum. There are Nullable versions (int? double? etc). Can be stored in heap if part of a reference type.
// These are value types for C#
// C# Type Keyword >NET type // bool System.Boolean // byte System.Byte // sbyte System.SByte // char System.Char // decimal System.Decimal // double System.Double // float System.Single // int System.Int32 // uint System.UInt32 // nint System.IntPtr // nuint System.UIntePtr // long System.Int64 // ulong System.UInt64 // short System.Int16 // ushort System.UInt16
Describe reference type data types.
A variable type which instead of storing the value in memory directly, stores the memory location of the actual data. Variable stores the memory reference of the data and not the data directly. Examples are strings, class, Arrays, etc
// Reference type // object System.Object // string System.String // dynamic System.Object
Name the 5 used data types.
int, double, string, bool, and char
Type casting is when you assign a value of one data type to another type.
What are ways to do this?
- Implicit Casting (automatically) - converting a smaller type to a larger type size
char -> int -> long -> float -> double
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9
- Explicit Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
Console.WriteLine(myDouble); // Outputs 9.78
Console.WriteLine(myInt); // Outputs 9
-Type Conversion Methods
Example: Convert.ToString(myInt)
- Parse Method
string testing = “2”;
int numTesting = int.Parse(testing);
Explain the TryParse() Method.
the TryParse method is used to safely convert a string to a specific data type (such as int, double, DateTime, etc.), without throwing an exception if the conversion fails. This is particularly useful when dealing with user input or data that may not be in the expected format.
Example:
Console.WriteLine(“Enter a number: “);
string? userInput = Console.ReadLine();
int number;
if(Int32.TryParse(userInput, out number))
{
Console.WriteLine(“Continue”);
}
else
{
Console.WriteLine(“Wrong Input”);
}
How would you ask a user for a input, lets say a number and print it as output.
Console.WriteLine(“Enter a number: “);
int num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($”Number: {num}”);
Operators are used to perform operations on variables and values.
….
Arithmetic Operators are used to perform common mathematical operations.
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
++ Increment x++
– Decrement x–
Assignment Operators are used to assign values to variables.
=, +=, -=, *=, /=, %=, &=, |=, ^=,»_space;=, <=
&=, AND - if it exist in both operands
|=, OR - if it exists in either operand
^=, XOR - bit if it is set in one operand but not both
«, Binary Left Shift Operator - The left operands value is moved left by the number of bits specified by the right operand
», Binary Right Shift Operator - The left operands value is moved right by the number of bits specified by the right operand
Comparison Operators are used to compare two values or variables. The return value of a comparison is either True or False.
== Equal x == y
!= Not Equal x != y
> Greater Than x > y
< Less Than x < y
>= Greater Than or Equal to x >= y
<= Less than or equal to x <= y
Logical Operators are used to determine the logic between variable or values.
&& - AND - Returns True if both statements are true
|| - OR - Returns True if one of the statements is true
! - NOT - Reverse the result, returns false if the result is true