C# Basic Structure, Identifiers, Data Types and Operators PDF

Summary

This document provides an introduction to C# programming fundamentals. It explains basic concepts such as data types, identifiers, and the structure of a C# console application. The document details the use of variables, operators, and comments.

Full Transcript

Computer Programming 3 Module 2: C# Basic Structure, Identifiers, Data Types and Operators Basic structure of a C# console program Line 1 using System; Line 2 namespace Greeting { Line 4 public class HelloWorld { Line 6 public...

Computer Programming 3 Module 2: C# Basic Structure, Identifiers, Data Types and Operators Basic structure of a C# console program Line 1 using System; Line 2 namespace Greeting { Line 4 public class HelloWorld { Line 6 public static void Main(string[] args) { Line 8 Console.WriteLine("Hello World!"); Line 9 } Line 10 } Line 11 } Basic structure explained Line 1: using System means that we can use classes from the System namespace. Line 2: namespace is a used to organize your code, and it is a container for classes and other namespaces. Line 5: class is a container for data and methods, which brings functionality to your program. Every line of code that runs in C# must be inside a class. In our example, we named the class Program. Line 6: Another thing that always appear in a C# program, is the Main method. Any code inside its curly brackets { } will be executed. You don't have to understand the keywords before and after Main. You will get to know them bit by bit while reading this tutorial. Line 9: Console is a class of the System namespace, which has a WriteLine() method that is used to output/print text. In our example it will output "Hello World!”. (Every C# statement ends with a semicolon (;) ). WriteLine or Write The most common method to output something in C# is WriteLine(), but you can also use Write(). The difference is that WriteLine() prints the output on a new line each time, while Write() prints on the same line (note that you should remember to add spaces when needed, for better readability): Comments Comments can be used to explain C# code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by C# (will not be executed). Multi-line comments start with. Any text between will be ignored by C#. Identifiers All C# variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume, areaOfTheCircle). (It is recommended to use descriptive names in order to create understandable and maintainable code.) Variables Variables are concerned with the storage of data. To use variables, you have to declare them. This means that you have to assign them a type and a name. Once you have declared variables, you can use them as storage units for the type of data that you declared them to hold. The general rules for naming variables are: Names can contain letters, digits and the underscore character (_) Names can begin with a letter, underscore or an @ symbol (Subsequent characters may be letters, underscore characters, or numbers) Names should start with a lowercase letter and it cannot contain whitespace Names are case sensitive Reserved words (like C# keywords, such as int or double) cannot be used as names int myNumber = 5; // Integer (whole number without decimals) double myFloatNumber = 5.99; // Floating point number (with decimals) char myLetter = 'D'; // Character string myText = "Hello"; // String (text) bool myBoolean = true; // Boolean (true or false) Data types A data type specifies the size and type of 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. The most common data types are: Data type Size Description int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits bool 1 bit Stores true or false values char 2 bytes Stores a single character/letter, surrounded by single quotes string 2 bytes Stores a sequence of characters, surrounded by double quotes per character Integer types Int The int data type can store whole numbers from -2,147,483,648 to 2,147,483,647. In general, the int data type is the preferred data type when we create variables with a numeric value. Long The long data type can store whole numbers from - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This is used when int is not large enough to store the value. Floating point types Float The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. It is 32-bit single-precision floating point type. It has 7 digit precision. To initialize a float variable, use the suffix f or F. Like, float number = 3.5F;. If the suffix F or f will not use then it is treated as double. Double The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. It is 64-bit double-precision floating point type. It has 14 – 15 digit precision. To initialize a double variable, use the suffix d or D. But it is not mandatory to use suffix because by default floating data types are the double type. Decimal The decimal type is a 128-bit data type suitable for financial and monetary calculations. It has 28-29 digit precision. To initialize a decimal variable, use the suffix m or M. Like as, decimal number = 300.5m;. If the suffix m or M will not use then it is treated as double. Boolean A boolean data type is declared with the bool keyword and can only take the values true or false. Characters The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c': Strings The string data type is used to store a sequence of characters (text). String values must be surrounded by double quotes: Operators Operators are used to perform operations on variables and values. Arithmetic Operators Arithmetic operators are used to perform common mathematical operations. Operator Name Description Example + Addition Adds together two values x+y - Subtraction Subtracts one value from another x-y * Multiplication Multiplies two values x*y / Division Divides one value by another x/y % Modulus Returns the division remainder x%y ++ Increment Increases the value of a variable by 1 x++ -- Decrement Decreases the value of a variable by 1 x-- Note: x and y variables was used to save space. Relational Operators Relational operators are used to compare two values. Operator Name Example == Equal to x == y != Not equal x != y > Greater than x>y < Less than x= Greater than or equal to x >= y

Use Quizgecko on...
Browser
Browser