Sie sind auf Seite 1von 7

Republic of the Philippines Courses Offered:

MARINDUQUE STATE
COLLEGE BS Industrial Technology
SCHOOL OF TECHNOLOGY BS Information
Technology
P. Manguera Sr., Rd.
BS Information System
Tanza, Boac, Marinduque

Worksheet No. 2: C# Basics

Name: Jayvee M. Rivas Date: 01-29-19


Year/Section: BSIT-3B Score: ________________________

General Directions: Read and answer the questions/statements properly. Write your answers on the box
provided. Save your file in Your_LastnameFirstname_Section.

1. What are the basic parts of a C# program structure? Provide an illustration.


 Namespace declaration Parts and Description
 A class A namespace is a collection of classes.
 Class methods A class contains the data and method definitions
 Class attributes that your program uses. Classes generally contain
 A Main method multiple methods. Methods define the behavior of
 Statements and Expressions the class.
 Comments A Main method states what the class does when
executed. It is the entry point for all C# programs.
Expression: An expression is a sequence of one or
more operands and zero or more operators that can
be evaluated to a single value, object, method, or
namespace. Expressions can consist of a literal
value, a method invocation, an operator and its
operands, or a simple name. Simple names can be
the name of a variable, type member, method
parameter, namespace or type.
Expressions can use operators that in turn use
other expressions as parameters, or method calls
whose parameters are in turn other method calls,
so expressions can range from simple to very
complex. Following are two examples of
expressions:

((x < 10) && ( x > 5)) || ((x > 20) && (x <
25));

System.Convert.ToInt32("35");

Something which evaluates to a value.


Example: 1+2/x
Statement:
The actions that a program takes are expressed in
statements. Common actions include declaring
variables, assigning values, calling methods,
looping through collections, and branching to one
or another block of code, depending on a given
condition. The order in which statements are
executed in a program is called the flow of control
or flow of execution. The flow of control may vary
every time that a program is run, depending on
how the program reacts to input that it receives at
run time.
A statement can consist of a single line of code
that ends in a semicolon, or a series of single-line

VISION: “Marinduque State College is a research-driven higher education institution pursuing excellence and innovation by 2025.”
MISSION: “Marinduque State College is committed to pursue progressive and innovative lifelong education founded on humanistic, professional and technologically
advanced programs across cultures and communities by establishing centres’ of excellence and development and research-drivenRevision
outreach No:
programs.”
3
RevisedRevision
Date:No: 0
08/5/2018
Revised Date: 08/05/2018
Republic of the Philippines Courses Offered:
MARINDUQUE STATE
COLLEGE BS Industrial Technology
SCHOOL OF TECHNOLOGY BS Information
Technology
P. Manguera Sr., Rd.
BS Information System
Tanza, Boac, Marinduque

statements in a block. A statement block is


enclosed in {} brackets and can contain nested
blocks
A line of code which does something.
Example: GOTO 100
Comments (/*….*/) are ignored by the compiler
and it is put to add comments in the program.

2. What methods are used for displaying text/output on screen which is considered are C# console output
statements? What are the differences between these methods?
Printing String using WriteLine()
Console.WriteLine("Prints on "); The main difference
Console.WriteLine("New line"); between WriteLine() and Write() is that
Output: the Write() method only prints the string provided
Prints on to it, while the WriteLine() method prints the string
New line and moves to the start of next line as well.
Printing String using Write()
Console.Write("Prints on ");
Console.Write("Same line");
Output:
Prints on Same line

3. What are the console input statements in C# which are used for capturing input from users? Discuss the
differences of each method.
Console.WriteLine() statement, Console.ReadLine() statement Console.ReadKey() statement
we display the message on the will read our input, but it is of is here just to keep our console
console window and move to the type string and what we need is window open.
next line. an int type.
So, we need to convert it with
the Convert. To
Int32() statement.

4. What is an identifier? How is it being used in C# programming?


An identifier, in C#, is the user-defined name of a program element. It can be a namespace, class,
method, variable or interface.
Identifiers are symbols used to uniquely identify a program element in the code. They are also
used to refer to types, constants, macros and parameters. An identifier name should indicate the
meaning and usage of the element being referred.
C# is a programming language that is compiled and has its implementation such that the
identifiers are only compile-time entities. During run time, each identifier will be referred by its
reference to the memory address and offset the compiler assigned to its textual identifier token.

5. What are the rules in creating identifiers in C#?

VISION: “Marinduque State College is a research-driven higher education institution pursuing excellence and innovation by 2025.”
MISSION: “Marinduque State College is committed to pursue progressive and innovative lifelong education founded on humanistic, professional and technologically
advanced programs across cultures and communities by establishing centres’ of excellence and development and research-drivenRevision
outreach No:
programs.”
3
RevisedRevision
Date:No: 0
08/5/2018
Revised Date: 08/05/2018
Republic of the Philippines Courses Offered:
MARINDUQUE STATE
COLLEGE BS Industrial Technology
SCHOOL OF TECHNOLOGY BS Information
Technology
P. Manguera Sr., Rd.
BS Information System
Tanza, Boac, Marinduque

The rules to be followed while using an identifier include:


 It can start only with a letter of the alphabet or underscore(_), but not a number
 It can be a combination of numbers, letters, connectors, Unicode escape sequence, etc.
 It cannot be a C# keyword
 It should not contain white space
 It cannot have more than 511 characters
 It has to be declared before it is referred
 It cannot have two consecutive underscores in its name because such identifiers are used for the
implementation
 More than one identifier with the same name cannot be declared within a single scope
An identifier prefixed with "@" is called a verbatim identifier. Although prefixing "@" allows
for the use of keywords, which helps in interfacing with other programming languages, it is not a
recommended practice.

6. What are keywords in C# programming? List some example of keywords in C# programming.


Keywords are predefined sets of reserved words that have special meaning in a program. The meaning
of keywords cannot be changed, neither can they be directly used as identifiers in a program.

C# has a total of 79 keywords. All these keywords are in lowercase. Here is a complete list of all C#
keywords.
abstract as base bool

break byte case catch

char checked class const

continue decimal default delegate

do double else enum

event explicit extern false

finally fixed float for

foreach goto if implicit

in (generic
in int interface
modifier)

internal is lock long

namespace new null object

out (generic
operator out override
modifier)

params private protected public

readonly ref return sbyte

VISION: “Marinduque State College is a research-driven higher education institution pursuing excellence and innovation by 2025.”
MISSION: “Marinduque State College is committed to pursue progressive and innovative lifelong education founded on humanistic, professional and technologically
advanced programs across cultures and communities by establishing centres’ of excellence and development and research-drivenRevision
outreach No:
programs.”
3
RevisedRevision
Date:No: 0
08/5/2018
Revised Date: 08/05/2018
Republic of the Philippines Courses Offered:
MARINDUQUE STATE
COLLEGE BS Industrial Technology
SCHOOL OF TECHNOLOGY BS Information
Technology
P. Manguera Sr., Rd.
BS Information System
Tanza, Boac, Marinduque

sealed short sizeof stackalloc

static string struct switch

this throw true try

typeof uint ulong unchecked

unsafe ushort using using static

void volatile while


7. What are the three types of Data Types in C#?
Types Description Data Types
 Value Value type variables
Type Represents Range
types can be assigned a
value directly. They
are derived from the
class System.ValueT bool Boolean value True or False
ype.
The value types
directly contain data. byte 8-bit unsigned integer 0 to 255
Some examples
are int, char, and
float, which stores char 16-bit Unicode character U +0000 to U +ffff
numbers, alphabets,
and floating point
numbers, decima 128-bit precise decimal
(-7.9 x 1028 to 7.9 x 10
respectively. When l values with 28-29
28
you declare significant digits
an int type, the
system allocates
memory to store the double 64-bit double-precision (+/-)5.0 x 10-324 to (+/-)1
value floating point type 10308

float 32-bit single-precision


-3.4 x 1038 to + 3.4 x 10
floating point type

int 32-bit signed integer type -2,147,483,648 to 2,147,4

long 64-bit signed integer type -9,223,372,036,854,775,8


9,223,372,036,854,775,80

sbyte 8-bit signed integer type -128 to 127

short 16-bit signed integer type -32,768 to 32,767

VISION: “Marinduque State College is a research-driven higher education institution pursuing excellence and innovation by 2025.”
MISSION: “Marinduque State College is committed to pursue progressive and innovative lifelong education founded on humanistic, professional and technologically
advanced programs across cultures and communities by establishing centres’ of excellence and development and research-drivenRevision
outreach No:
programs.”
3
RevisedRevision
Date:No: 0
08/5/2018
Revised Date: 08/05/2018
Republic of the Philippines Courses Offered:
MARINDUQUE STATE
COLLEGE BS Industrial Technology
SCHOOL OF TECHNOLOGY BS Information
Technology
P. Manguera Sr., Rd.
BS Information System
Tanza, Boac, Marinduque

uint 32-bit unsigned integer


0 to 4,294,967,295
type

ulong 64-bit unsigned integer


0 to 18,446,744,073,709,5
type

ushort 16-bit unsigned integer


0 to 65,535
type

 Refer The reference types Object Type


ence do not contain the The Object Type is the ultimate base class for all data types in
types actual data stored in C# Common Type System (CTS). Object is an alias for
a variable, but they System.Object class. The object types can be assigned values of
contain a reference to any other types, value types, reference types, predefined or user-
the variables. defined types. However, before assigning values, it needs type
In other words, they conversion.
refer to a memory When a value type is converted to object type, it is
location. Using called boxing and on the other hand, when an object type is
multiple variables, converted to a value type, it is called unboxing.
the reference types
can refer to a Dynamic Type
memory location. If You can store any type of value in the dynamic data type
the data in the variable. Type checking for these types of variables takes place
memory location is at run-time.
changed by one of Dynamic types are similar to object types except that type
the variables, the checking for object type variables takes place at compile time,
other variable whereas that for the dynamic type variables takes place at run
automatically reflects time.
this change in value.
Example of built- String Type
in reference types The String Type allows you to assign any string values to a
are: object, dynamic variable. The string type is an alias for the System.String class.
, and string. It is derived from object type. The value for a string type can be
assigned using string literals in two forms: quoted and @quoted.

 Point Pointer type


er variables store the
types memory address of
another type.
Pointers in C# have
the same capabilities
as the pointers in C
or C++.

8. What are the two kinds of C# type conversion? Provide programming example.
Type Conversion Description Example Conversion
Implicit type conversion These conversions are performed // Implicit conversion. A
by C# in a type-safe manner. For long can
example, are conversions from // hold any value an int can

VISION: “Marinduque State College is a research-driven higher education institution pursuing excellence and innovation by 2025.”
MISSION: “Marinduque State College is committed to pursue progressive and innovative lifelong education founded on humanistic, professional and technologically
advanced programs across cultures and communities by establishing centres’ of excellence and development and research-drivenRevision
outreach No:
programs.”
3
RevisedRevision
Date:No: 0
08/5/2018
Revised Date: 08/05/2018
Republic of the Philippines Courses Offered:
MARINDUQUE STATE
COLLEGE BS Industrial Technology
SCHOOL OF TECHNOLOGY BS Information
Technology
P. Manguera Sr., Rd.
BS Information System
Tanza, Boac, Marinduque

smaller to larger integral types hold, and more!


and conversions from derived
classes to base classes. int num = 2147483647;
long bigNum = num;
Explicit type conversion These conversions are done using System;
explicitly by users using the pre-
defined functions. Explicit namespace
conversions require a cast TypeConversionApplication
operator. {
class ExplicitConversion
{
static void Main(string[]
args) {
double d = 5673.74;
int i;

// cast double to int.


i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
Output:
5673
9. Enumerate the built-in C# type conversion methods.
C# type conversion methods Description and Example
ToBoolean Converts a type to a Boolean value, where possible.
using System;

namespace TypeConversionApplication {
class StringConversion {
static void Main(string[] args) {
int i = 75;
float f = 53.005f;
double d = 2345.7652;
bool b = true;

Console.WriteLine(i.ToString());
Console.WriteLine(f.ToString());
Console.WriteLine(d.ToString());
Console.WriteLine(b.ToString());
Console.ReadKey();

}
}
}
Output:
75
53.005
2345.7652
True

VISION: “Marinduque State College is a research-driven higher education institution pursuing excellence and innovation by 2025.”
MISSION: “Marinduque State College is committed to pursue progressive and innovative lifelong education founded on humanistic, professional and technologically
advanced programs across cultures and communities by establishing centres’ of excellence and development and research-drivenRevision
outreach No:
programs.”
3
RevisedRevision
Date:No: 0
08/5/2018
Revised Date: 08/05/2018
Republic of the Philippines Courses Offered:
MARINDUQUE STATE
COLLEGE BS Industrial Technology
SCHOOL OF TECHNOLOGY BS Information
Technology
P. Manguera Sr., Rd.
BS Information System
Tanza, Boac, Marinduque

ToByte Converts a type to a byte.


ToChar Converts a type to a single Unicode character, where
possible
ToDateTime Converts a type (integer or string type) to date-time
structures
ToDecimal Converts a floating point or integer type to a decimal
type.
ToDouble Converts a type to a double type.
ToInt16 Converts a type to a 16-bit integer.
ToInt32 Converts a type to a 32-bit integer.

Prepared By:

SGD. Kierven A. Villaruel


Instructor 1

VISION: “Marinduque State College is a research-driven higher education institution pursuing excellence and innovation by 2025.”
MISSION: “Marinduque State College is committed to pursue progressive and innovative lifelong education founded on humanistic, professional and technologically
advanced programs across cultures and communities by establishing centres’ of excellence and development and research-drivenRevision
outreach No:
programs.”
3
RevisedRevision
Date:No: 0
08/5/2018
Revised Date: 08/05/2018

Das könnte Ihnen auch gefallen