0% found this document useful (0 votes)
22 views12 pages

Fundamental Concepts of C#

Uploaded by

e.mutindamalusi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views12 pages

Fundamental Concepts of C#

Uploaded by

e.mutindamalusi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CMT 307-.

NET PROGRAMMING

Chapter 3: Introduction to C# C# can be used for designing a variety of enterprise


applications that run on the .NET Framework. With the
3.1. Introduction language, programmers van create a wide spectrum of
This chapter aims at introducing learners to C# programming solutions such web applications, Windows Forms-based
language. We look at fundamental concepts of C #
applications, windows phone applications e.t.c.
programming such as memory allocations in C#, data types
supported by C#. We shall also discuss the various types of
It is described as a “...simple, modern, object-oriented, and
operators supported by C#. Finally, we provide a summary for
type-safe programming language derived from C and C++”
the chapter and some self-testing exercise for the learners
which bears many syntactic similarities to C++ and Java.
3.2. Introduction to C# language
Ideally, not all of the supported languages fit entirely neatly 3.3. Identifiers & keywords
into the .NET framework, but the one language that is Identifiers refers to a name given to various program elements
guaranteed to fit in perfectly is C#. It is successor to C++ was such as variables, functions and arrays. Keywords refers to
released in conjunction with the .NET framework. those reserved words which have special meaning in
C# such as namespace, for, while, do e.t.c

The language was developed by Anders Hejlsberg and Scott Rules for naming identifiers
Wiltamuth at Microsoft lab. C# has strong roots in C, C ++ and 1. They can contain letters, digits, and _.
Java adapting best features of each language. It provides
2. can only start with a letter or _
many features associated with .NET such Object-oriented and
powerful class library of pre-built components. 3. Spaces are not allowed

1|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

4. Keywords/reserved words cannot be used as variable 3.5. C# data types


names C# being a strongly typed language means that you cannot
5. They are case sensitive use variable without data types. Before using identifiers in any

Conventions of naming identifiers programming, programmers need to declare the identifiers.

1. Identifiers should be short. The identifiers need to be assigned a data type during their
definition. These identifiers can have different data type
2. Identifiers should be descriptive and meaningful.
depending on the kind of data they will store.
3. Variables should be in small letters.
A data type is a set of values and the allowable operations on
4. Constant variables should be written in capital.
those values. C# provides all the data types that are available
3.4. Comments in C# in Java, and adds support for unsigned numerals and a new
C# supports single line and multi-line comments. All 128-bit high-precision floating-point type. Those data types
characters available inside any comment are ignored by the have different sizes and storing capacities.
compiler. C# multiple comments start with /* and end with */
while single line comment starts with two forward slash (//) as All primitive data types in C# are objects in the System
depicted by the example that follows. namespace. For each data type, a short name (alias) is
/* This is a single comment */ provided. For instance, int is the short name for System.Int32
/* C # comments can also span multiple lines */ and double is the short form of System.Double.
In C#, variables are categorized into the following types:

 Value types

2|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

 Reference types 3.5.3 Pointer Types


They are variables that store the memory address of another
 Pointer types
type. Pointers in C# have the same capabilities as in C or C++.
3.5.1 Value Types
3.5.4 C# built in data type
These are variables can be assigned a value directly. They
All primitive data types in C# are objects in the System
are derived from the class System.ValueType. They also
namespace. The list of C# data types and their aliases is
directly contain data. Examples include int, char, and float,
provided in the following table. As you can see, the first eight
which stores numbers, alphabets, and floating point numbers,
of these correspond to the primitive types available in Java.
respectively. When you declare an int type, the system
Java's Boolean is called bool in C#.
allocates memory to store the value.
3.5.4.1. Integers
3.5.2 Reference Types This type of data type is a subset of the real numbers written
This type does not contain the actual data stored in a variable, without a fraction or a decimal component.
but they contain a reference to the variables that is they refer
C# .NET Type Size in Range
to a memory location. Using more than one variable, the
Alias bits
reference types can refer to a memory location. If the data in
the memory location is changed by one of the variables, the
sbyte System.SByte 8 -128 to 127
other variable automatically reflects this change in value.
Example of built-in reference types are: object, dynamic and byte System.Byte 8 0 to 255

string. short System.Int16 16 -32,768 to 32,767

ushort System.UInt16 16 0 to 65,535

3|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

int System.Int32 32 -2,147,483,648 to C#


.NET Type Size Precision Range
2,147,483,647 Alias

uint System.UInt32 32 0 to 4,294,967,295 1.5 x 1045


System.
float 4 bytes 7 digits to 3.4 x
long System.Int64 64 - Float
1038
9,223,372,036,854,775,
5.0 x 10324
808 to System. to
double 8 bytes 15-16 digits
9,223,372,036,854,775, Double 1.7 x
807 10308
ulong System.UInt64 64 0 to 28-29 decimal 1.0 x 1028
System.
Table 2.0C# integer data types decimal 16 bytes places to 7.9 x
Decimal
3.5.4.2. Floating point numbers 1028

They are used represent real numbers in computing. Real Table 2.1 C# floating data types

numbers measure continuous quantities, like weight, height or 3.5.4.3. Strings and chars

speed. In C# we have three floating point types: float, double A string in C# is a sequence of Unicode characters while a
and decimal. char is a single Unicode character. Strings are enclosed by
double quotes or the @ symbol while a char is enclosed in
single quote as shown below

string s=”This is a string”;

4|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

Char gender=’M”; 3.5.4.6. Bool data type

It is used for storing Boolean values i.e. true/ false e.g.


Bool status=”true”;
3.5.4.4. Datetime

The Datetime is a value type. It represents an instant in time,


typically expressed as a date and time of day. For example to The user-defined reference types are includes class,
display the current system date and time, we can use the interface, or delegate, enumerations.
following statement
3.6. C# Variables
Current Time:
These are identifiers whose value is allowed to change
Time=DateTime.Now.ToString ("HH:mm:ss"); Current Date: during run time. A variable must be declared before being
Date=DateTime.Today.ToString ("dd-MM-yyyy"); used in a program. Declaring a variable means you are
requesting the operating system for a piece of memory. This
3.5.4.5. Object Type
piece of memory you give a name and you can store
They are the ultimate base class for all data types in C#
something in that piece of memory.
Common Type System (CTS). Object is an alias for System.
3.6.1 Declaring variables in C#
Object class. So object types can be assigned values of any
To declare variables in C#, you need to specify the data type
other types, value types, reference types, predefined or user
followed by the name of the variable which should follow the
defined types. However, before assigning values, it needs type
rules of naming identifiers. You can also initialize the variable
conversion.
with a value during declaration but this is optional.
The general syntax must take the form:

5|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

Variable_Data_type variable _name= [initialization value]; Const float PI=3.142;/ *declare a constant called
Variable definition can take the form: PI and assign the value of 3.142 to it*/

Type variableName1, variableName2. . . VariableNameN; const char choice=’A’;

Where the variable are of the same data type 3.8. Assignment in C#
We use the assignment operator (=) to
e.g.
assign a variable/constant a value .Assignment is
Float base, height, areaCircle; always done from left to right. The general format is as follows
Int myAge, number_throws; Variable=expression;
Where expression is the value we are assigning to the variable
3.7. Constants in C# though the assignment operator looks like the mathematical
A constant is an identifier whose value is not expected to equality operator, In C# it’s a meaning is different. The
change during program execution. assignment statement simply indicates that the value on the
3.7.1 Defining constants right hand side of the assignment operator must be
Just like a variable, we need to declare a constant before we stored/assigned to the variable named on the left hand side.
can use it in a program. To declare a constant in c sharp, we
use the keyword const in the definition. The following general
Ideally, the operator should be read as “becomes equal to” and
syntax
means that the variable on the left hand side has its value
Const Data type const_name= [expression];
changed to the value of the expression on the right hand side.
For example to declare a variable named PI of type float, we
For the assignment to work successfully, the type of the
can use the following statement

6|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

variable on the left hand side should be the same as the type C# comes with many built-in operators that allow a programmer
returned by the expression. to manipulate data. An operator performs a function on one or
For example, for example a program that adds two numbers more operands. For example, we can add two variables with
together the "+" addition operator and store the result in a third variable

Float a, b, sum=0; with the "=" assignment operator like this:


Int x, y, z ;
a=Convert.ToFloat (TxtNum1.Text);
b=Convert.ToFloat (TxtNum2.Text); Z = x+y;
sum=a+b; The two variables (x, y) are called operands. There are
MessageBox.show (“The sum of the two numbers in” +sum); different types of operators supported by C# and they are
3.9. Statements in C# described below in the order of their precedence.
A statement is meant to cause the computer to carry out some i). Arithmetic Operators
action. All C# statements must be terminated with a semicolon
ii). Relational Operators
except for comments and control structure statements and
function. iii). Logical Operators

3.10. Operators in C# iv). Bitwise Operators


An operator is a symbol that tells the compiler to perform
v). Assignment Operators
specific mathematical or logical manipulations. The same
operators supported by C, C++ and Java are also supported by This module shall examine the arithmetic, relational, logical
the C# programming language.
&assignment operators into detail.

7|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

3.10.1 Arithmetic Operators operands satisfy the comparison. C# programming language


C# as a programming language supports the following supports the following relational operators. Assume we have
arithmetic operators. Assume we have two variables; x holds two variables; x holds 15 and y holds 10, then;
15 and y holds 10, then; Operator Description Example
Operator Description Example == Checks if the values of two (X==Y) is not true
+ Adds two operands X+y will yield 25 operands are equal or not, if

- Subtracts second operand from x-y will yield 5 yes then the condition

the first becomes true

* Multiplies two operands X*y will yield 150 > Checks if the value of left (X>Y) is true
operand is greater than the
/ Divides two operands x/y will yield 1
value of right operand, if yes
% Modulus Operator and X% y will yield 5
then condition becomes true.
remainder of after an integer
< Checks if the value of left (X<Y) is not true
division
operand is less than the
++ Increment operator, increases X++ will yield 16
value of right operand, if yes
integer value by one
then condition becomes true.
-- Decrement operator, decreases Y++ will yield 9
>= Checks if the value of left (X>=Y) is true
integer value by one
operand is greater than or
3.10.2 Relational/comparison Operators
equal to the value of right
They are used perform some comparison between two
operands and return a Boolean value indicating whether the

8|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

to the value of right operand,


Operator Description Example if yes then condition becomes

&& Called Logical AND operator. If (X&&Y) is false true.

both the operands are non-zero 3.10.3 Logical Operators

i.e. The table above provides a list of logical operators supported

then the condition becomes by C# language. The result of logical operators can only be a

true. The result of logical operators can only be a Boolean value

|| Called Logical OR Operator. If (X||Y) is true either true or false. Assume variable x holds 1 and variable y

any of the two operands is holds 0, then:

nonzero, then condition 3.10.4 Assignment Operators

becomes true. C# supports the following assignment operators. Assume x

! Called Logical NOT Operator. !(X||Y) is false hold 15 and y holds 10, then;

Used to reverses the logical Operator Description Example


= It’s known as the z = X+y will assign
state of its operand. If a
assignment operator. It value of X+y into z
condition is true, then Logical
assigns values from right i.e z will be 25
NOT operator will make false.
side operands to left side
operand, if yes then condition
operand
becomes true.
<= Checks if the value of left (X<=Y) is not true
operand is less than or equal

9|CHAPTER THREE- BASIC CONCEPTS C#


CMT 307-.NET PROGRAMMING

+= It’s known as AND x += y is 3.10.5 Concatenation operators


assignment operator. It equivalent to x = x String concatenation is defined for operands of type String only.
adds right operand to the +y The result is a string that consists of the characters from the
left operand and assign the first operand followed by the characters from the second
result to left operand operand. The + (plus) characters signify string concatenation.
-= Its known as Subtract AND x -= y is 3.10.6 2. Misc Operators
assignment operator, It equivalent to x = x There are few other operators supported by C# Language
subtracts right operand -y
operator Operator description
from the left operand and
Delete memory release operator
assign the result to left
New memory allocation operator
operand
Cast Casting operators which are used to convert one
*= Its known as Multiply AND x *= y is
data type to another. For example,
assignment operator, It equivalent to x = x
Float sum=ToDouble(TxtSum.text); will assign a
multiplies right operand *y
value of float to sum
with the left operand and
(dot) Known as member operators and are used to
assign the result to left
associate an object with its property or method
operand
3.11. Type casting and conversions
/= Its known as Divide AND x /= y is equivalent
Type casting means converting one type of data to another
assignment operator, It to x = x /y
type. Just like Java, C# supports both implicit and explicit type

10 | C H A P T E R T H R E E - B A S I C C O N C E P T S C #
CMT 307-.NET PROGRAMMING

conversions. Both Java and C# follow similar rules for the 3.12. VB.Net VS C#.Net
automatic conversion and casting of data types. VB.Net is the latest release of Microsoft’s Visual Basic
3.11.1. Implicit type conversion language. VB is an event driven programming language
derived heavily from BASIC programming language. VB
These conversions are performed by C# in a type-safe manner.
enables Rapid Application Development (RAD) of graphical
Examples are conversions from smaller to larger integral types
user interface (GUI) applications
and conversions from derived classes to base classes. A
classic example is provided below which converts an integer
value to a long integer C# (C-Sharp) is also a Microsoft product which is object-
Int a = 5; oriented, and type-safe programming language derived from C
Long b = a; //implicit conversion and C++ bearing many syntactic similarities to C++ and

3.11.2. Explicit type conversion Java.

The differences of the 2 languages lie in:


Can be achieved in two ways where the target datatype
precedes the name of variable as shown below  Syntax

Long a = 5483; intb = (int)a; //explicit  Object Oriented Features


conversion
Syntax differences
Explicit type casting can also be achieved using pre-defined
VB.Net and C#.NET have a great difference in their syntax.
methods. The methods are listed in the table that follows
These include just naming a few: Line termination, code
commenting, namespace usage and declaration

11 | C H A P T E R T H R E E - B A S I C C O N C E P T S C #
CMT 307-.NET PROGRAMMING

3.13. Chapter exercise


3.14. Further reading suggestions
3.15. Chapter summary
In this chapter we have discussed a number of fundamental
concepts surrounding c#. We have discussed the various data
types supported by C# programming language. Any time we
define a new identifier, it has to be assigned a valid data type.
Various data types have a specific size they occupy in
memory though that size can vary from architecture to
architecture. We have also looked at the various operators
supported by the language and how they can be used.

12 | C H A P T E R T H R E E - B A S I C C O N C E P T S C #

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy