Fundamental Concepts of C#
Fundamental Concepts of C#
NET PROGRAMMING
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. 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
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
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*/
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
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
- Subtracts second operand from x-y will yield 5 yes then the condition
* 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
then the condition becomes by C# language. The result of logical operators can only be a
|| Called Logical OR Operator. If (X||Y) is true either true or false. Assume variable x holds 1 and variable y
! Called Logical NOT Operator. !(X||Y) is false hold 15 and y holds 10, then;
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
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
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 #