CSCI 3328 Object Oriented CSCI 3328 Object Oriented Programming in C# Programming in C#
Chapter 1: Introduction to C#Chapter 1: Introduction to C#
Xiang Lian
The University of Texas Rio Grande Valley
Edinburg, TX 78539
1
Objectives
• In this chapter, you will– Know requirements of this course
– Recall basic components in a computer system
– Learn the evolution of programming language
– Refresh your memory about the object-oriented programming
– Become aware of the .Net Framework for Visual C#
2
Background Required
• C++ Programming– If you made an A in 1370/1170 you will do fine
with some effort
– If you have already taken 2380, you will find this course not very difficult
• You need to be able to look up how to get things done (for example, look up syntax)
3
Programming, Programming, Programming
• This class is unlike other courses in that you will spend a lot of time doing actual programming
• You should never copy a program from someone else• Even if your program does not perform as good as
someone else’s, you should submit what you have
• You should not hesitate to ask questions in class
4
Assignments
• Unless I announce it, programs are due in one week after assignment is given in class, or posted • Occasionally, I will give two weeks for difficult
programs
• You should work on a program the same day it is assigned• That way you can ask questions during the
following class period
5
Use of the Textbook
• I will not be covering chapter by chapter
• I will cover important points needed to complete the assignments
• You need to search the index in the back to find the topic needed for each assignment
• You can also obtain help from MSDN at the Microsoft site
6
Versions of Visual C#
• You can program using VC# 2008, 2010, or 2012
• Our book only covers 2008 – If you use 2010 or 2012, you may need to find
help from other sources or the Web
– Usually, there are not many differences for programming with a higher version of Visual C#
7
The Time for Class
8
• I expect to hold the class for little over an hour each time
• Remaining time should be spend with your assigned groups to discuss the project in class
• Please do not exchange programs with each other, just ideas
9
CSCI 1380: Elements of a Computer System
• Hardware– CPU
–Main memory
– Secondary storage
– Input / Output devices
• Software– System programs
– Application programs (e.g. Visual Studio)
10
Programming Languages
• Machine language
• Assembly language
• High-level language– E.g., C#
11
Object Technology
• We humans are very good in recognizing and working with objects, such as a pen, a dog, or a human being
• We learned to categorize them in such a way that make sense to us. –We may categorize them as animate object,
inanimate objects, pets, friends, etc.
12
Object Technology (cont'd)
• We some times classify objects based on their attributes, for example, green apples or red apples, fat or slim people, etc.
• If you think about it each object has many attributes. – If I ask you list the attributes of an orange, you
probably could list many things such as color, shape, weight, smell, etc.
13
Object Technology (cont'd)
• In addition to attributes, all objects exhibit behaviors.
• A dog eats, barks, wags its tail, plays, and begs. – A dog exhibits many more other behaviors than
this short list.
• Another thing we need to remember about objects is that objects interact between each other
14
Object Technology in Visual C#
• Visual C#– Object-oriented • E.g. forms, labels, buttons, radios, checkboxes, etc.
– Properties of objects• E.g. background color of forms
– Event-driven• Behaviors events of objects
• E.g. clicks
15
.Net Framework
• A software platform• Language-neutral• Designed for cross-language compatibility– Applications written in C# may reference a DLL
file written in any other language
• 2 components:– Common Language Runtime (CLR)– Class Libraries
16
The .Net Framework
• Underneath the applications you develop using C# is the .Net Framework containing a library of classes needed for windows forms and web forms.
• The .Net framework interacts with the Common Language Runtime (CLR), which in turn interacts with the operating system and the hardware
• This way programs you write using C# can run on different operating systems and hardware platforms as long as it has the correct version of the .Net installed on it
17
Common Language Runtime
• Execution Engine of .Net• Manages the execution of programs and
provides core services:– Code compilation–Memory allocation– Thread management– Garbage collection
18
Class Libraries
• Designed to integrate with the common language runtime
• This library gives the program access to runtime environment
• The class library consists of lots of prewritten code that all the applications created in VC# .Net and Visual Studio .Net will use
• The code in Visual C # for all the elements like forms, and controls actually comes from the class library
19
Namespaces
• Within the .Net Framework, related classes are grouped into Namespaces
• You need to insert a “using AppropriateNamespace” in your program
• After that you can refer to methods and attributes of classes contained therein
20
To Get .Net Namespace Documentation
• Visit:http://msdn.microsoft.com/en-us/library/ms229335.aspx
21
How Do You Write and Run a Program?
• Use the Visual Studio to create a project• File New Project…• The project file will have the extension *.csproj. The project stores all
related files, including the source file you write. Every project has a solution file with the extension *.sln. You would be opening this file when you want to return to your program you were writing. This will open all associated files. The associated files may be contained in a subdirectory.
• After writing the source code, go to Build and build your solution, which creates the intermediate code
• Go to Debug and start without debugging• The program run will finish and give you a prompt to press any key to
continue. Starting with debugging, it will complete the run and return to you to the IDE. If your IDE does offer “without debugging”, add a Console.ReadKey() statement.
22
Options for a New Project
• Console application
• Windows forms application
• …
23
Hello Programusing System;//using System.Collections.Generic;//using System.Linq;//using System.Text;
namespace sayHello{ class sayHello { static void Main(string[] args) {
Console.WriteLine("Hello!"); Console.WriteLine("Welcome to Dr. Lian's C# Class!"); Console.ReadLine();
} }} 24
Using the Form Designer
• You can size the form by holding the handles in the corners or the middle
• Controls and other items are added to the form using the toolbox• The control or item has properties that can be set
• To write the code for an event, you can click on the view code button above the solution explorer or by simply clicking on a button or anything else on the form
• Files with code you write will have extension .cs• Files ending with designer.cs are code files generated by
Visual Studio
25
Naming Controls and Boxes
• Use a convention you will remember• You can start all button names with btn• An accept button will be named btnAccept• An exit button will be named btnExit
• All text boxes will start with txt • Sales amount will be named txtSalesAmount
• All labels with start with lbl• lblEnterName
• It is up to you to choose naming conventions you will recognize
26
Your First Assignment
• Assignment #1
• The purpose of this assignment is to practice reading and writing to console using C#. It is a simple assignment. Use the following program run to design and write your program. Please submit Program listing and a screen capture of the program run. Follow the textbook examples to add comments as documentation.
27
Assignment Submission
• Make sure to include your name, assignment number, date due and date completed on each assignment you submit
• Assignment should be submitted in a manila folder with your name and assignment number appearing on the tab
• Submit your assignments to the Blackboard
28
Exercises
• Give a brief explanation of the .NET framework
• What is the Common Language Runtime?
• How do you place “Total Square Feet: 1238” in a text box? Assume that 1238 in an integer stored in a variable called sqFt.
29