Sunteți pe pagina 1din 36

Overview

C# is a modern, general-purpose, object-oriented programming


language developed by Microsoft and approved by Ecma and ISO.

C# was developed by Anders Hejlsberg and his team during the


development of .Net Framework

C# is designed for Common Language Infracture (CLI), which


consists of the executable code and runtime environment that
allows use of various high-level languages to be used on different
compute platforms and architectures.

The following reasons make C# a widely used professional language:

- Modern, general-purpose programming language


- Object-oriented.
- Component oriented
- Easy to learn
- Structured language
- It produces efficient programs.
- It can be compiled on a variety of computer platforms
- Part of .Net Framework.

Strong Programming Features of C#

Although c# constructs closely follow traditional high-level


languages C and C++ and being an Object-Oriented programming
language, it has strong resemblance with Java, it has numerous
strong programming features that make it endearing to multitude of
programmers worldwide.

1.
Integrated Development Environment (IDE) for C#

Microsoft provides the following development tools for C#


programming

- Visual Studio 2010 (VS)


- Visual C# 2010 Express (VCE)
- Visual Web Developer

The last two are freely available from Microsoft official website.
Using these tools, you can write all kinds of C# programs from
simple command-line applications to more complex applications.
You can also write C# source code files using a basic text editor, like
notepad, and compile the code into assemblies using the command-
line compiler, which is gain a part of the .Net Framework.

Visual C# express and Visual Web Developer Express edition are


timed down versions of Visual Studio and has the same look feel.
They retain most features of Visual Studio.

2.
C# - Program Structure

A C# program basically consists of the following parts:

- Namespace declaration
- A class
- Class methods
- Class attributes
- A main method
- Statements & expressions
- Comments

Let us look at a simple code that would print the words “Hello
World”.

Using system;
Namespace HelloWorldApplication
{
Class HelloWorld
{
Static void main(String[] args)
{
/* my first program in C# */
Console.WriteLine(“Hello World”);
Console.ReadKey();
}
}
}

When the above code is compiled and executed, it produces the


following Result:

Hello World

3.
Let us look at various parts of the above program:

- The first line of the program using System;- the using


keyword is used to include the systemnamespace in the
program. A program generally has multiple using
statements.
- The next line has the namespace declaration. A
namespace is a collection of classes. The
helloworldapplication namespace contains the class
helloworld.
- The next line has a class declaration, the class
helloworld contains the data and method definitions
that your program uses. Classes generally would contain
more than one method. Methods define the behavior of
the class. However, the helloworld class has only one
methodMain.
- The next line defines the Main method, which is the
entry point for all C# programs.
- The Main method states what the class will do when
executed
- The next line /*…*/ will be ignored by the compiler and
it has been put to add additional comments in the
program
- The Main method specifies its behavior with the
statement console.writeline(“Hello World”);
- Writeline is a method of the console class defined in the
system namespace. The statement causes the message
“Hello, World!” to be displayed on the screen.
- The last line Console.readkey(); is for the VS.NET users.
This makes the program wait for a key press and it
prevents the screen from running and closing quickly
when the program is launched from Visual Studio .Net.

4.
Date Types:

the value types deirectly contain data. Some examples are Int, Char,
Float, which stores numbers, alphabets, floating point numbers,
respectively. When you declare an Int type, the system allocates
memory to store the value.

The following table lists the available value types in C# 2008:


Type Represents Range Default
Value
Bool Boolean Value True or False False
Byte 8-bit unsigned integer 0 to 255 0
Char 16-bit Unicode character U +0000 to U +ffff ‘\0’
Decima 128-bit precise decimal (-7.9 x 10 to 7.9 x 10)/10 to 0.0M
l values with 28-29 28
significant digits
Double 64 bit double-precision (+/-)5.0 x10 to (+/-)1.7 x 10 0.0D
floating point type
Float 32-bit single-precision -3.4 x 10 to +3.4 x 10 0.0F
floating point type
Int 32-bit signed integer type -2,147,483,648 to 0
2,147,483,647
Long 64-bit signed integer type -923,372,036,854,774,808 0L
to
9,223,372,036,854,775,807
Sbyte 8-bit signed integer type -128 to 127 0
Short 16-bit signed integer type -32,768 to 32,767 0
Uint 32-bit unsigned integer 0 to 4,294,967,295 0
type
Ulong 64-bit unsigned integer 0 to 0
type 18,446,744,073,709,551,61
5
Ushort 16-bit unsigned integer 0 to 65,535 0
type

5.
Operators
An operator is a sysmbol that tells the compiler to perform specific
mathematical or logical manipulations. C# is rich in built-in
operators and provides the following type of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
Arithmetic Operators
+ Adds to Operands A + B will give
30
- Subtracts second operand from first A-B will give -10
* Multiplies both operands A*B will give
200
/ Divides numerator by de-numerator B/A will give 2
% Modulus operator and remainder of after an B%A will give 0
integer division
++ Increment operator increases integer value by one A++ will give 11
-- Decrement operator decreases integer value by A—will give 9
one
Relational Operator
== Checks if the values of two operands are equal or not, if yes then condition (A==B)i
becomes true s not
true
!= Checks if the values of two operands are equal or not, if values are not (A!=B)i
equal then condition becomes true s true
> Checks if the value of left operand is greater than the value of right (A>B) is
operand, if yes then condition becomes true not
true
< Checks if the value of left operand is less than the value of right operand, if (A<B)is
yes then condition becomes true true
>= Checks if the value of left operand is greater than or equal to the value of (A>=B)i
right operand, if yes then condition becomes true s not
true
<= Checks if the value of left operand is less than or equal to the value of right (A<=B)i
operand, if yes then condition becomes true s true

6.
Logical Operator
&& Called logical and operator. If both the operands (A&&B)
are non zero then condition becomes true is false
|| Called logical OR operator. If any of the two (A||B)i
operands is non zero then condition becomes s True
true.
Called Logical NOT operator. Use to reverses the !(A&&
! logical state of its operand. If a condition is true B)is
then logical NOT operator will make false true.

Bitwise Operator
& Binary AND operator copies a bit to (A & B)will give 12.
the result if it exists in both Which is 0000 1100
operands.
| Binary OR operator copies a bit if it (A|B) will give 61,
exists in either operand. which is 0011 1101
ᶺ Binary XOR operator copies the bit if (AᶺB)will give 49,
it is set in one operand but not both.which is 0011 0001
~ Binary Ones complement operator is (~A)will give -61,
unary and has the effect of flipping which is 1100 0011
bits in 2’s complement
due to a signed
binary number
<< Binary left shift operator. The left A<<2 will give 240,
operands value is moved left by the which is 1111 0000
number of bits specified by the right
operand
>> Binary right shift operator. The left A>>2 will give 15,
operands value is moved right by the which is 0000 1111
number of bits specified by the right
operand.

7.
Assignment Operators

= 8.Simple assignment operator, assigns values C=A+B will assign


from right side operands to left side operand value of A+B into
C
+= Add AND assignment operator, it adds right C+=A is equivalent
operand to the left operand and assign the to C=C+A
result to left operand
-= Subtract AND assignment operator, it C-=A is equivalent
subtracts right operand from the left operand to C=C-A
and assign the result to left operand
*= Multiply AND assignment operator, it C*=A is equivalent
multiplies right operand with the left operand to C=C*A
and assign the result to left operand
/= Divide AND assignment operator, it divides left C/=A is equivalent
operand with the right operand and assign the C=C/A
result to left operand
%= Modulus AND assignment operator, it takes C%=A is
modulus using two operands and assign the equivalent to
result to left operand C=C%A
<<= Left shift AND assignment operator C<<=2 is same as
C=C<<2
>>= Right shift AND assignment Operator C>>=2 is same as
C=C>>2

8.
Decision Making

Decision making structures require that the programmer specify


one or more conditions to be evaluated or tested by the program,
along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements
to be executed if the condition is determined to be false.

Following is the general from of a typical decision making structure


found in most of the programming languages:

Condition

Conditional code

9.
C# Provides following types of decision making statements

If statement An if statements consists of a Boolean


expression followed by one or more
statements.
If…else statement An if statement can be followed by an
optional else statement, which executes
when the Boolean expression is false.
Nested if statement You can use one if or else if statement inside
another if or else if statement(s).
Switch statement A switch statement allows a variable to be
tested for equality against a list of values.
Nested switch statements You can use one switch statement inside
another switch statement(s).

Loops

There may be a situation, when you need to execute a block of code


several number of times in general statements are executed
sequentially: the first statement in a function is executed first,
followed by the second, and so on.

Programming languages provide various control structures that


allow for more complicated executions paths.

A loop statement allows us to execute a statement or group of


statements multiple times and following is the general from of a
loop statement in most of the programming languages:

10.
Conditional code

If condition is true
Condition

If condition is false

C# provides following types of loop to handle looping requirements.

While loop Repeats a statement or group of statements


while a given condition is true. It tests the
condition before executing the loop body.
For loop Executes a sequence of statements multiple
times and abbreviates the code that manages
the loop variable.
Do…while loop Like a while statement, except that it tests the
condition at the end of the loop body
Nested loops You can use one or more loop inside any
another while, for or do..while loop

11.
Windows Form Application

A form is a bit of screen real estate, usually rectangular, that


you can use to present information to the user and to accept
input from the user. Forms can be standard windows, multiple
document interface (MDI) windows, dialog boxes, or display
surfaces for graphical routines. The easiest way to define the
user interface for a form is to place controls on its surface.
Forms are objects that expose properties which define their
appearance, methods which define their behavior, and events
which define their interaction with the user. By setting the
properties of the form and writing code to respond to its events,
you customize the object to meet the requirements of your
application.

As with all objects in the .NET Framework, forms are instances


of classes. The form you create with the Windows Forms
Designer is a class, and when you display an instance of the
form at run time, this class is the template used to create the
form. The framework also allows you to inherit from existing
forms to add functionality or modify existing behavior. When
you add a form to your project, you can choose whether it
inherits from the Form class provided by the framework, or
from a form you have previously created.

Additionally, forms are controls, because they inherit from the


Control class.Within a Windows Forms project, the form is the
primary vehicle for user interaction. By combining different
sets of controls and writing code, you can elicit information
from the user and respond to it, work with existing stores of
data, and query and write back to the file system and registry
on the user's local computer.

12.
Partial Classes

There are several situations when splitting a class definition is


desirable:

 When working on large projects, spreading a class over


separate files enables multiple programmers to work on
it at the same time.
 When working with automatically generated source,
code can be added to the class without having to
recreate the source file. Visual Studio uses this
approach when it creates Windows Forms, Web service
wrapper code, and so on. You can create code that uses
these classes without having to modify the file created
by Visual Studio.

public partial class Employee


{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}The partial keyword indicates that other parts of the class,
struct, or interface can be defined in the namespace. All the
parts must use the partial keyword. All the parts must be
available at compile time to form the final type. All the parts
must have the same accessibility, such as public, private, and
so on.
.

13.
Windows Form Application I.D.E

The Visual C# integrated development environment (IDE) is a


collection of development tools exposed through a common user
interface.

 The Code Editor, for writing source code.


 The C# compiler, for converting C# source code into an
executable program.
 The Visual Studio debugger, for testing your program.
 The Toolbox and Designer, for rapid development of user
interfaces by using the mouse.
 Solution Explorer, for viewing and managing project files and
settings.
 Project Designer, for configuring compiler options, deployment
paths, resources, and more.
 Class View, for navigating through source code according to
types, not files.
 Properties Window, for configuring properties and events on
controls in your user interface.
 Object Browser, for viewing the methods and classes available in
dynamic link libraries including .NET Framework assemblies
and COM objects.
 Document Explorer, for browsing and searching product
documentation on a local computer and on the Internet.

14.
Designing a User Interface

Adding Controls

In either designer, you use the mouse to drag controls, which are
components with visual representation such as buttons and text
boxes, onto a design surface. The following illustration shows a
combo box that has been dragged from the Toolbox window onto a
form in the Windows Forms Designer.

As you work visually, the Windows Forms Designer translates your


actions into C# source code and writes them into a project file that
is named name.designer.cs where name is the name that you gave
to the form. Similarly, the WPF designer translates actions on the
design surface into Extensible Application Markup Language (XAML)
code and writes it into a project file that is named Window.xaml.
When your application runs, that source code (Windows Form) or
XAML (WPF) will position and size your UI elements so that they
appear just as they do on the design surface

15.
Setting Properties Project 2: Grading System

After you add a control to the design surface, you can use the Output:
Properties window to set its properties, such as background
x
color and default text.
Student Name:: Total Grades:
In the Windows Form designer, the values that you specify in
the Properties window are the initial values that will be Student Number: Equivalent:
assigned to that property when the control is created at run
time. In the WPF designer, the values that you specify in the Course: Remarks:
Properties window are stored as attributes in the window's
XAML file. Prelim: Compute

In many cases, those values can be accessed or changed Midterm: New


programmatically at run time by getting or setting the property
on the instance of the control class in your application. The Finals: Print
Properties window is useful at design time because it enables
you to browse all the properties, events, and methods supported
Formula:
on a control.
Total Grades=(prelim + midterm + finals)/3
Equivalent:
Events Handler Grades Equivalent
100-99 - 1.00
Programs with graphical user interfaces are primarily event-driven. 98-96 - 1.25
They wait until a user does something such as typing text into a text 93-95 - 1.50
box, clicking a button, or changing a selection in a list box. When 90-92 - 1.75
that occurs, the control, which is just an instance of a .NET 87-89 - 2.00
Framework class, sends an event to your application. You have the 84-86 - 2.25
option of handling an event by writing a special method in your 81-83 - 2.50
application that will be called when the event is received. 78-80 - 2.75
75-77 - 3.00
You can use the Properties window to specify which events you 74-below - 5.00
want to handle in your code. Select a control in the designer and Remarks:
click the Events button, with the lightning bolt icon, on the Total Grade>=75 - Passed
Properties window toolbar to see its events. The following icon Total grade<=75 - Failed
shows the events button.

16.
Button

A command button is a clickable image object used with


graphical operating systems. For example, in Microsoft
Windows, when clicking the close button (the X in the top-
right of the window), Windows closes that open window.

Textbox

A text box, text field or text entry box is a graphical control


element intended to enable the user to input text information to be
used by the program.

Label

A label control is used to display text that a user cannot edit


directly. However, you can write code to change the text that is
displayed by assigning a string value to the label's Text
property. Common uses of the label control are to display a
message to the user and to identify another control (such as a
textbox) so that the user has an idea of what should be
entered into that textbox.

Combo Box

A combo box is a commonly used graphical user interface


widget (or control). Traditionally, it is a combination of a drop-
down list or list box and a single-line editable textbox, allowing
the user to either type a value directly or select a value from
the list.

List Box

A list box is a graphical control element that allows the user to


select one or more items from a list contained within a static,
multiple line text box. The user clicks inside the box on an item
to select it, sometimes in combination with the ⇧ Shift or Ctrl in
order to make multiple selections.
Picture Box

PictureBox provides a rectangular region for an


image. It supports many image formats. It has an
adjustable size. It can access image files from your
disk or from the Internet. It can resize images in
several different ways.

Timer

Timer regularly invokes code. Every several seconds


or minutes, it executes a method. This is useful for
monitoring the health of an important program, as
with diagnostics.
Common Properties of C#.NET Controls

BackColor Specifies the background color of the control.


BorderStyle Generally, specifies whether or not a control has
a border.
Text The string of text that is to be displayed in the
control.
Enabled Determines whether or not the control can
respond to user-generated events.
Font For controls displaying text, specifies the font
(name, style, size, etc.) to be applied to the
displayed text.
ForeColor Specifies the color of text or graphics to be
displayed in the control.
Height Specifies the height of the control in pixels.
Left Specifies the distance (in pixels) between the
internal left edge of a control and the left edge of
its containter.
Name The string value used to refer to the control in
code.
Image Specifies the graphic to be displayed in the
control.
TabIndex Specifies the tab order of a control within its
parent form,
TabStop Specifies whether or not the user can use the Tab
key to give focus to the control.
Tag A string containing extra data associated with the
control.
Top Specifies the distance (in pixels) between the
internal top edge of a control and the top edge of
its containter.
Visible Specifies whether the control is visible or hidden.
Width Specifies the width of the control in pixels.
Exercise 1: Create Simple Calculator

Output:

Input 1st Number:


Input 2nd Number:

Addition:
Subtraction:
Multiplication:
Division:

Formula:
Addition: 1st Number + 2nd Number
Subtraction: 1st Number - 2nd Number
Multiplication: 1st Number * 2nd Number
Division: 1st Number / 2nd Number
Exercise 3: input 1st number and 2nd number then display to Using system;
messagebox the sum of two input number, subtract the two input Namespace Application1
number, multiply the two input number and divide the two input {
number. follow the figure below: Class program
{
x Static void main(string[] args)
{

Enter 1st Number: 1

Enter 2nd Number:


1

Compute
x

Addition: 2 Subtraction: 0 Multiply: 1 Divide: 1

Exercise 4: Input age to the textbox and display to another textbox


if the input age is qualified to vote or to young.

Enter age: 18 }
}
Remarks: Qualified to vote }

Code:
Click
Exercise 2: Reverse 3 digit Number Windows Form Application

Output: Exercise 1: input your name and click the click button then show to
messagebox the input name

Input 3 Digit Number:


x
Reverse:

Codes:
Namespace ConsoleApplication1 Enter Your Name: Your name
{
Class Program
{
Click
Static void main(String[] args)
{ x

Hello….Your Name

Exercise 2: input your name and click the click button then show to
Another Textbox the input name

Enter Your Name: Your name


}
}
} Hello………

Click
Exercise 3: Reverse 4 digit Number

Output:

Input 4 Digit Number:


Reverse:

Codes:
Namespace ConsoleApplication1
{
Class Program
{
Static void main(String[] args)
{

}
}
}
} Exercise 4: Reverse 5 digit Number
}
} Output:
Input 5 Digit Number:
Reverse:

Codes:
Namespace ConsoleApplication1
{
Class Program
{
Static void main(String[] args)
{

}
}
}
Exercise 5: College Billing

Output:

Saint John Bosco


Guadalupe, Makati City
-
Student Name:
Student Number:
-
Tuition Fee:
Down Payment:
-
Balance:
-
Breakdown of Balance
Prelim:
Midterm:
Finals:

Formula:
Balance=Tuition Fee – Down Payment

Prelim=balance/3
Midterm=balance/3
Finals=balance/3
Codes:
Namespace ConsoleApplication1
{
Class Program
{
Static void main(String[] args)
{

}
}
}
Exercise 5: College Billing

Output:

Saint John Bosco


Grading System
-
Student Name: Course:
Student Number: Session:
-
Prelim:
Midterm:
Finals:

Total Grades:
Equivalent:
Remarks:

Formula:
Total Grades=(prelim + midterm + finals)/3
Equivalent:
Grades Equivalent
100-99 - 1.00
98-96 - 1.25
93-95 - 1.50
90-92 - 1.75
87-89 - 2.00
84-86 - 2.25
81-83 - 2.50
78-80 - 2.75
75-77 - 3.00
74-below - 5.00
Remarks:
Total Grade>=75 - Passed
Total grade<=75 - Failed
Codes:
Namespace ConsoleApplication1
{
Class Program
{
Static void main(String[] args)
{
}
}
}
Exercise 7: Voting Program

Output:

Voting Program
Input Age:

Remarks:

If Age>=18 && age<=80


Remarks: “Qualified to Vote”
If age<=17 && age>=1
Remarks: “Too Young”
Else
Remarks: “Out of Range”
Codes:
Namespace ConsoleApplication1
{
Class Program
{
Static void main(String[] args)
{

}
}
}
Exercise 8: Simple Payroll System

Output:

Saint John Bosco


Payroll System
-
Employee’s Name:
Position:

Rate per Hour:


Total Hour:

Basic Pay:

SSS(10%):
Philhealth(20%):
Pag-ibig(30%):

Total Deduction:

Net Pay:

Formula:
Basic Pay=Rate per hout*Total Hour

SSS=basic pay *10/100


Philhealth=basic pay *20/100
Pag Ibig=basic pay *30/100

Total Deduction=(SSS+Philhealth+Pagibig)

Net Pay=Basic Pay-Total Deduction


Codes:
Namespace ConsoleApplication1
{
Class Program
{
Static void main(String[] args)
{
}
}
}
Exercise 9: Leap Year Project 1: Merge all Activity in one Program

Output: Output:
Leap Year/Not Leap Year Saint John Bosco
Input a year to check if it is leap year or not leap year List of Activity

Input Year: 1: Simple Calculator


2: Reverse 3 Digit Number
Remarks: 3: Reverse 4 Digit Number
4: Reverse 5 Digit Number
Codes:
5: College Billing
Namespace ConsoleApplication1
6: Grading System
{
7: Voting Program
Class Program
8: Simple Payroll System
{
9.: Leap Year
Static void main(String[] args)
10: Exit
{
Enter Selection:

Codes:
Namespace ConsoleApplication1
{
Class Program
{
Static void main(String[] args)
{

}
}
}

S-ar putea să vă placă și