You are on page 1of 4

SH1803

C# Basic Concepts
I. What is C#?
 It is an elegant object-oriented language that enables developers to build a variety of secure
and robust applications that run on the .NET Framework.
 It can be used to create Windows applications, Web services, mobile applications, client-
server applications, database applications, and much, much more.

A. The .NET Framework


 A framework that consists of two (2) parts:
o The Common Language Runtime (CLR) is the foundation of the .NET Framework.
It manages code at execution time, providing core services such as memory
management, code accuracy, and many other aspects of your code.
o The Class Library is a collection of classes, interfaces, and value types that enable
you to accomplish a range of common programming tasks, such as data collection,
file access, and working with text.
 C# programs particularly use the .NET Framework class library extensively to do
common tasks and provide various functionalities.

II. Variables
 Allows programs to use data for performing tasks.
 Reserves a memory location (a space in memory) for storing values.
 Information stored in the location can be changed while the program is running, hence its
name.
 To use them, they must first be declared by specifying a name and a data type.
o Variable names, also known as identifiers, can contain letters, numbers, and the
underscore character ( _ ); identifiers can only start with a letter or underscore.
o Although the name of a variable can be any set of letters and numbers, the best
identifier is descriptive of the data it will contain; this is very important in order to
create clear, understandable and readable code.

A. Variable Types
 Also known as data types.
 They define the information that can be stored within a variable, the size of the needed
memory and the operations that can be performed with the variable.

B. Built-in Data Types in C#


 int – used for whole numbers
 float – used for decimal numbers and floating-point numbers
 double – used for larger values than the float data type can accommodate
 char – used for a single alphanumeric character
 bool – used to variables that accept only Boolean values: True or False
 string – used for saving sequences of characters

III. Printing Text


 Text (such as those found instring values) can be displayed on the console window via the
Console.Write() or Console.WriteLine() methods.
o Console.Write() will continue to print text in the same line so long as the method is
continuously called.

01 Handout 1 *Property of STI


 student.feedback@sti.edu Page 1 of 4
SH1803

o Console.WriteLine() ends its printed text with a line terminator, which moves the cursor
to the next line; similar subsequent methods will print their text in their own respective
lines.

IV. Getting User Input


 Users can be prompted to enter data using the Console.ReadLine() method.
 Entered input is assigned to a string variable.
 Input values can be converted into other data types using the Convert.ToXXX method
(where XXX is the .NET name of the data type that the value will be converted into).
o Convert.ToDouble will convert the string input into double data type values.
o Convert.ToBoolean will convert input into Boolean values.
o Convert.ToInt16, Convert.ToInt32 and Convert.ToInt64 converts input to integer
values based on specified bit size of the integer (16-bit, 32-bit, and 64-bit, respectively).

V. Comments
 Explanatory statements that you can include in a program to benefit the reader of your
code.
 The compiler ignores everything that appears in the comment, so none of the comment’s
contents affect the result.
 A comment beginning with two slashes (//) is called a single-line comment. The slashes
tell the compiler to ignore everything that follows, until the end of the line.

A. Multi-Line Comments
 Comments that require multiple lines.
 Denoted by the symbols /* at the beginning of the comment block, and end with */ at
the end of the comment block.
 They can be placed on the same line or inserted with one or more lines between them.

VI. The var Keyword


 A handy C# alternative function that enables the compiler to determine the type of the
variable automatically based on the expression it is assigned to.
 Any resulting value that uses this function adapts its respective data type without manual
declaration.
 Variables declared using the keyword are called implicitly-typed variables.
 Implicitly typed variables MUST have a value upon initialization; any other method of
initialization may cause an error.

VII.Constants
 Used to store a value that CANNOT be changed after their initial assignment.
 Uses the const modifier for its declaration.
 Useful for storing values that are unchanging or consistent (such as days of the week, or
the value of pi).

VIII. Arithmetic Operators


 Symbols that perform manipulations on specified values.
 Most operators are mostly arithmetic or logical by nature.

01 Handout 1 *Property of STI


 student.feedback@sti.edu Page 2 of 4
SH1803

A. Supported Operators

SoloLearn.com

B. The Division Operator (/)


 Divides the first operand by the second operand.
 Any remainder is dropped in order to return an integer value.

C. The Modulus Operator (%)


 Informally known as the remainder operator.
 Returns the remainder of an integer division instead of the actual resulting value.

D. Operator Precedence

 Determines the grouping of terms in an expression, which affects the sequence in which
an expression is evaluated.
 Certain operators take higher precedence over others; for example, the multiplication
operator has higher precedence than the addition operator.
 Usage of parentheses (( )) can alter the precedence of operators in any given expression:
Operations within parentheses are performed first; if there are parenthetical expressions
nested within one another, the expression within the innermost parentheses is evaluated
first.
 Operator precedence in programming follows the same rules of precedence in general
mathematics.

IX. Assignment and Increment Operators

A. Assignment Operators
 Assigns the value on the right side of the operator to the variable on the left side.
 Denoted by the equal sign (=).
 C# also provides Compound Assignment Operators that perform an operation and an
assignment in one statement; this is usually done by adding the appropriate arithmetic
operator before the equal sign.

01 Handout 1 *Property of STI


 student.feedback@sti.edu Page 3 of 4
SH1803

B. The Increment Operator (++)


 Used to increase an integer's value by one (1)
 It is considered as one of the most commonly used C# operators.

C. Increment Prefix and Postfix


 The increment operator has two (2) forms:
o The prefix form increments the value, and then proceeds with the expression; it is
written by placing the increment operator BEFORE the variable to be incremented.
o The postfix form evaluates the expression and then performs the incrementing; it is
written by placing the increment operator AFTER the variable to be incremented.

D. The Decrement Operator (--)


 Works in much the same way as the increment operator, but instead of increasing the
value, it decreases it by one (1).
 Also follows the increment operator’s rules for prefix and postfix.

References:
 SoloLearn. (n.d.). C# tutorial. Retrieved from https://www.sololearn.com/Course/CSharp/
 Microsoft (n.d.). Introducing Visual Studio.NET. Retrieved May 2016 from
http://msdn.microsoft.com/en-uslibrary/fc6bk1f4(v=vs.71).aspx.

01 Handout 1 *Property of STI


 student.feedback@sti.edu Page 4 of 4

You might also like