You are on page 1of 41

Introduction to C#

Review Quiz
1. public interface IPaint:Ifigure
{ void Paint();}
View the problem statement and answer the following
question.
What is the fully qualified name of the Paint method?
1) IFigure.Paint
2) IPaint.Paint
3) void Paint
4) IFigure.IPaint.Paint

©NIIT Introduction to C# /Slide 1 of 41


Introduction to C#

Review Quiz (Contd.)


2. A class implements five interfaces, one base interface
and four derived interfaces. How many times can you
implement the base interface?
1) five
2) zero
3) one
4) four

©NIIT Introduction to C# /Slide 2 of 41


Introduction to C#

Review Quiz (Contd.)


3. An interface IFigure contains two methods, Draw and
Paint. The interface IPolygons, which is derived from
the interface IFigure, contains three members:
Square, Pentagon, and Hexagon. A class Shape
implements the interface IPolygons. Which interface
members are implemented by the class Shape?
1) Square, Pentagon, and Hexagon
2) Draw and Paint
3) Paint, Pentagon, and Hexagon
4) Paint, Draw, Square, Pentagon, and Hexagon

©NIIT Introduction to C# /Slide 3 of 41


Introduction to C#

Review Quiz (Contd.)


4. An interface IPainter contains two methods, Draw and
Paint. A class Rectangle, which is derived from the
class Shape, implements the interface IPainter. The
class Rectangle implements the Paint method and the
class Shape implements the Draw method.
Which class(es) is/are involved in interface mapping of
the class Rectangle?
1) Rectangle
2) Shape
3) Shape and Rectangle
4) None of the given classes
©NIIT Introduction to C# /Slide 4 of 41
Introduction to C#

Review Quiz (Contd.)


5. Which access modifiers can you assign to a struct? (2
or more options.)
1) public
2) abstract
3) private
4) sealed

©NIIT Introduction to C# /Slide 5 of 41


Introduction to C#

Review Quiz (Contd.)


6. What is the data type of a struct variable?
1) Value type
2) Reference type
3) Pointer type
4) Array type

©NIIT Introduction to C# /Slide 6 of 41


Introduction to C#

Review Quiz (Contd.)


7. Which data types can you specify as the underlying
types for an enum? (2 or more options)
1) int
2) char
3) float
4) ulong
5) double

©NIIT Introduction to C# /Slide 7 of 41


Introduction to C#

Review Quiz (Contd.)


8. Which access modifiers can you assign to an enum?
(2 or more options)
1) public
2) protected
3) abstract
4) new
5) sealed

©NIIT Introduction to C# /Slide 8 of 41


Introduction to C#

Review Quiz (Contd.)


9. You create two Windows Forms named frmEmployee
and frmOrganization. Which statement sets
frmEmployee as a child form of frmOrganization?

1) frmEmployee.MDIParent = frmOrganization;
2) frmEmployee.MDIFormName = frmOrganization;
3) frmOrganization = DIContainer.add(frmEmployee);
4) frmEmployee.ParentForm = frmOrganization;

©NIIT Introduction to C# /Slide 9 of 41


Introduction to C#

Review Quiz (Contd.)


10. Which class provides the basic methods, events, and
properties to all the derived ActiveX controls?
1) Controls
2) RichTextBoxControl
3) RichControl
4) BaseControl

©NIIT Introduction to C# /Slide 10 of 41


Introduction to C#

Answers to Review Quiz


1. 2 9. 1
2. 3 10. 3
3. 4
4. 3
5. 1,3
6. 1
7. 1,2,and 4
8. 1,2,and 4

©NIIT Introduction to C# /Slide 11 of 41


Introduction to C#

Objectives
In this lesson, you will learn to:
 Write and execute a basic C# program
 Write a program with variables and statements
 Create methods with parameters
 Create classes and namespaces
 Specify attributes for an object
 Create user-defined data types
 Implement interfaces
 Create WinForms

©NIIT Introduction to C# /Slide 12 of 41


Introduction to C#

Objectives (Contd.)
 Handle events, delegates and exceptions
 Handle files
 Access a database

©NIIT Introduction to C# /Slide 13 of 41


Introduction to C#

Write and Execute a Basic C# Program


 Introducing C#
 C# is an object-oriented programming language
developed for the .NET platform
 C# is part of Microsoft Visual Studio .NET
 C# has evolved from C++
 C# supports features such as garbage collection,
versioning, language interoperability, and COM
 File extension for a C# file is .cs

©NIIT Introduction to C# /Slide 14 of 41


Introduction to C#

Write and Execute a Basic C# Program (Contd.)


 Writing a basic program in C#
 A C# program comprises four primary elements, a
namespace declaration, a class declaration, a
Main method, and a program statement
 The method Main is the start point of all C#
applications
 The method System.WriteLine displays the output
on the screen
 The method System.ReadLine accepts inputs from
the user

©NIIT Introduction to C# /Slide 15 of 41


Introduction to C#

Write and Execute a Basic C# Program (Contd.)


 Compiling and executing a C# program
 The C# compiler is invoked by typing csc followed
by the filename of the source file
 You can compile the source code with a variety of
options that allow you to specify the output file,
input file, code generation, warning, and levels for
the compiled code
 You execute a C# program by running the .exe file
created after compiling the source code
 The bugreport compiler option stores the errors
generated on compiling a source in a file

©NIIT Introduction to C# /Slide 16 of 41


Introduction to C#

Write a Program with Variables and Statements


 Identifying data types in C#
 Data types represent the characteristics, such as
size and range, of the data in memory
 Value and reference are the two categories of data
types in C#
 C# supports implicit and explicit conversion

©NIIT Introduction to C# /Slide 17 of 41


Introduction to C#

Write a Program with Variables and Statements


(Contd.)
 Declaring variables in C#
 C# categorizes variables into three types based on
their scope and availability
 The three categories of variables in C#
 Static
 Instance
 Array

©NIIT Introduction to C# /Slide 18 of 41


Introduction to C#

Write a Program with Variables and Statements


(Contd.)
 Using statements and expressions in a C# program
 C# supports the following statements:
 if
 goto
 for
 for..each
 switch
 while
 do..while

©NIIT Introduction to C# /Slide 19 of 41


Introduction to C#

Create Methods with Parameters


 Defining methods
 A method is a member of a class or an object,
which allows you to implement an action
 A method consists of a header and a body
 A method includes the new, public, static, virtual,
sealed, override, abstract, and extern modifiers
 The return-type of a method specifies the type of
the value returned by the method
 C# includes eight types of modifiers that can be
used to declare a method

©NIIT Introduction to C# /Slide 20 of 41


Introduction to C#

Create Methods with Parameters (Contd.)


 Identifying types of parameters
 Parameters are used to pass data to a method
 There are four types of parameters: value,
reference, output, and params

©NIIT Introduction to C# /Slide 21 of 41


Introduction to C#

Create Classes and Namespaces


Creating classes
 Classes are used to group related objects together
 Class modifiers can be public, private, protected,
internal, abstract, sealed, or new
 Classes contain both the class members that are
declared in the class and that are inherited from
the direct base class
 The fields, methods, properties, events, operators,
constructors, and destructors of a class are class
members

©NIIT Introduction to C# /Slide 22 of 41


Introduction to C#

Create Classes and Namespaces (Contd.)


Assigning properties elements
 Properties are the named attributes of a class
 Properties have the get and set accessors that
read or write the value of a property
 Properties can be declared with a virtual or an
abstract modifier specified with both the get and
set accessors
 Properties can delay the initialization of a resource

©NIIT Introduction to C# /Slide 23 of 41


Introduction to C#

Create Classes and Namespaces (Contd.)


Declaring namespaces
 Namespaces reuse and organize code
 Namespaces are included in a program by
declaring the namespace with the using keyword

©NIIT Introduction to C# /Slide 24 of 41


Introduction to C#

Specify Attributes for an Object


 Assigning attributes to a class
 Attributes are declared in sections and multiple
attributes are declared in lists separated by
commas
 Attributes have attribute name and optionally
include positional parameters and named
arguments
 An attribute can be used multiple times by using
the named parameter AllowMultiple

©NIIT Introduction to C# /Slide 25 of 41


Introduction to C#

Specify Attributes for an Object (Contd.)


 Identify the types of attributes for a class
 The preprocessing identifier of the conditional
attribute enables you to control the execution of a
method
 Obsolete attributes mark class members that are
no longer required by the class

©NIIT Introduction to C# /Slide 26 of 41


Introduction to C#

Create User-Defined Data types


 Defining structures
 User-defined data types can be created using the
struct keyword
 Constructors can be overloaded to initialize the
struct members
 Methods can be included to overload operators in
the struct declaration

©NIIT Introduction to C# /Slide 27 of 41


Introduction to C#

Create User-Defined Data types (Contd.)


 Defining enumerations
 Define enumerations by using the enum keyword
 Assign associated values to the members of
enums
 Convert the enum type to the underlying integral
type

©NIIT Introduction to C# /Slide 28 of 41


Introduction to C#

Implement Interfaces
 Overview of Interfaces
 An interface provides a set of functionality for a
program
 Classes and structs implement an interface
 An interface member can be referred to by using
its fully qualified name in a class

©NIIT Introduction to C# /Slide 29 of 41


Introduction to C#

Implement Interfaces (Contd.)


 Implementing members of an interface
 Interface members need to be defined in the class
that implements the interface
 A class can implement more than one interface
 Derived interfaces can be explicitly implemented
with the base interface in a class

©NIIT Introduction to C# /Slide 30 of 41


Introduction to C#

Implement Interfaces (Contd.)


 Mapping an interface
 Linking an interface to its implementation in a class
is called interface mapping
 Interface mapping is essential because the
functionality of the interface is not available to
client programs unless the interface members are
mapped to their respective implementations

©NIIT Introduction to C# /Slide 31 of 41


Introduction to C#

Implement Interfaces (Contd.)


 Re-implementing an interface
 Multiple classes can re-implement an interface in a
program
 The functionality provided by the interface can be
customized by re-implementing an interface

©NIIT Introduction to C# /Slide 32 of 41


Introduction to C#

Create WinForms
Creating forms and adding controls to the form
 Create the following by using the WinForms
namespace
 Windows Forms
 Buttons
 Text Boxes
 Labels

©NIIT Introduction to C# /Slide 33 of 41


Introduction to C#

Create WinForms (Contd.)


Creating ActiveX controls
 Create ActiveX controls
 Test the design time functionality of ActiveX
controls by using the WinDes application

©NIIT Introduction to C# /Slide 34 of 41


Introduction to C#

Handle Events, Delegates and Exceptions


 Handle events
 Events are declared by using the event keyword
 Delegates act as an intermediary between the
source application and the destination event
 Events are invoked only from the class that
contains the event declaration

©NIIT Introduction to C# /Slide 35 of 41


Introduction to C#

Handle Events, Delegates and Exceptions


(Contd.)
Using delegates to handle events
 Delegates are used to forward the calls to the
methods
 Delegates are safe
 Delegates are used to handle events and callbacks

©NIIT Introduction to C# /Slide 36 of 41


Introduction to C#

Handle Events, Delegates and Exceptions


(Contd.)
Handling exceptions
 Exceptions are the errors that are generated during
the execution of a program
 You use the throw statement to throw exceptions
 You use the try-catch statement to handle
exceptions

©NIIT Introduction to C# /Slide 37 of 41


Introduction to C#

Handle Files
Accessing files
 Input and output operations are performed on files
by using streams
 Streams are used for two types of file access,
random and sequential
 The FileStream class is used to access files for
read and write operations

©NIIT Introduction to C# /Slide 38 of 41


Introduction to C#

Handle Files (Contd.)


Performing read write operations
 The TextReader and TextWriter namespaces are
used to perform read and write operations on files
 The TextWriter namespace includes the
StreamWriter class
 The TextReader namespace includes the
StreamReader class
 The StreamWriter class is used to write to a stream
 The StreamReader class is used to read from a
stream

©NIIT Introduction to C# /Slide 39 of 41


Introduction to C#

Access a Database
 Connecting to a database
 ADO.NET provides data access functionality to
communicate with data sources, such as Microsoft
SQL Server 2000
 ADO.NET is independent of data sources
 ADO.NET provides better performance than ADO
 ADO.NET consists of DataSet and managed
provider

©NIIT Introduction to C# /Slide 40 of 41


Introduction to C#

Access a Database (Contd.)


 Modifying records in a table
 The Insert statement is used to insert records in a
DataSet
 The Delete statement is used to delete records
from a DataSet
 The Update statement is used to update changes
to a table from a DataSet to a database

©NIIT Introduction to C# /Slide 41 of 41

You might also like