Introduction to programming using C#
Question 1
An object is a ___ of a class.
Property
object
method
instantiation
QUESTION 2
Fields within a class should be public.
True
False
QUESTION 3
Properties or instance methods, within a class, should be public.
True
False
QUESTION 4
How many arguments can be used in a Constructor header?
1
10
Any amount.
0
QUESTION 5
The default constructor
both
none of these
sets numeric fields to 0
has empty parameters
10 points Saved
QUESTION 6
Assume that you have created a class named Student with a constructor defined as Student(int
age). Which of the following overloaded constructors could coexist with the Chair constructor
without ambiguity?
none of these
Student(int age, string name)
Student(int age)
both
QUESTION 7
A collection of methods that can be used by any class, as long as the class provides a definition
to override the collection's definitions is
a class
a superclass
an interface
a template
QUESTION 8
When you create a class and want to include the capability to compare its objects so they can use
the Array.Sort() or Array.BinarySearch() method, you must
Use the CompareTo() Method
Create a destructor
Create a constructor
Use the IComparable () method
QUESTION 9
You can use the instance variable field as the identifier for an instance method.
Example:
private int age;
public int age { get; set; }
True
False
QUESTION 10
Arrays can hold references to objects.
True
False
Module Nine
Class Concepts
Concepts / Vocabulary
• Classes can be considered application • Instance Variables (known as fields)
programs that contain a Main ()
hold object attributes or data
method.
components for a class.
• Classes can hold objects that can be
• Instance Methods:
instantiated by another class.
• Associated with objects and every
Everything is essentially an object or a
instance of the class will have the same
member of another class because
methods.
objects are one property or one
element of a class.
Concepts / Vocabulary
• Class Header:
• Creating a Class:
• Like methods, it has an optional access
class ClassName
modifier. A method default access
modifier is private but a class default is
{
internal.
• We us the keyword class to create a
class, followed by an identifier or name
for the class.
• Also like a method, we can use other
access modifiers such as public,
protected, internal and private.
// Instance variables and
// methods can go inside
//the braces.
}
Concepts / Vocabulary
Module Nine
Instance Variables and Methods
Concepts / Vocabulary
When a class is created, we also define
the attributes and properties of the
class. This would include the variables
or fields of a class, properties or
objects of the class and methods.
When we use private fields within a class,
it is considered information hiding.
The fields of a class have their own set
of access modifiers that include: new,
public, protected, internal, private,
static, readonly, and write.
This allows you to control outside access
to data by controlling private data from a
public method.
*Most fields will be private because they
offer the highest level of security.
Although not always the case, it would be
good to make the class methods public.
Concepts / Vocabulary
We are able to use the variable idNumber even though it is private because we made the
method public. So we can pull a private source from a public source and is why we make
methods public. You can use the variable with better security this way.
Concepts / Vocabulary
• Has-a Relationship:
• Defines the instance in which a class has
objects of another class.
• Composition:
• Using an object within an object.
• If this was a bank program, you would
have a bank account class buy you
would also have a class for actual
customers that use objects from the
bank account class.
Module Nine
Creating Objects
Concepts / Vocabulary
• Creating an Object:
• When you create or declare a class, it
does not contain or come pre-loaded
with objects. All objects must be
created.
• To create an object we must:
• 1) Declare an object type and a name
(identifier).
• 2) Create the object.
• Reference Type:
• The identifier we use for the object is
how we reference its location or
memory address.
• When we create an object we can call
its constructor.
Concepts / Vocabulary
• Class Object Example:
Employee bob = new Employee ();
Bob.WelcomeMsg();
• Passing Objects to Methods:
• You can pass objects to methods just
like other data types:
• Example:
(in main)
Module Nine
Properties
Concepts / Vocabulary
• Property:
• The member of a class that provides access
to a field. A Get and Set will be defined for
setting and retrieving field information.
• The setter and getter are known as
accessors. The “set” sets the value for a field
and the “get” returns the stored value of a
field.
• Properties are linked to an object using the
“.”.
• Ex. student.Age;
• Field Example:
• private int age;
• Property Example:
Concepts / Vocabulary
• Auto-Implemented Property:
• The implementation or the set/get are
automatically created for you. You do
not need to declare the field that
corresponds with the property.
Class Student
{
public int Age (get; set;)
public string Name (get; set;)
}
Module Nine
This reference and public/private data
Concepts / Vocabulary
• Why care about the Accessibility?
• When Programming, data should be
hidden whenever possible, you never
really want information to be public if
you can help it. Instead, information
should be accessed by accessors.
• Public data fields can be used when all
the objects of a class contain the same
value.
• You can create a constant variable with
a class and it will always be static or it
will always belong to the entire class,
rather than a particular instance.
Concepts / Vocabulary
• Understanding the “this” Reference
• The This reference is an implicitly
passed reference.
• It tells the method which instance or
“scenario” of the class we are going to
use.
• You can also pass a parameter to a
this.reference.
Module Ten
Using Contructors
Concepts / Vocabulary
• Constructors:
• A special method that instantiates an
object. There is a default constructor
that automatically supplies a
constructor with no parameters.
• The default values of an object are
initialized with a default constructor.
• Book book1 = new Book();
• A default constructor takes no
arguments; however, you are able to
set information directly into them.
• public Book()
{
ReaderName = “Nicole”;
}
Concepts / Vocabulary
• Passing Parameters:
• You can create constructors that
receive different arguments. In fact, you
can create several constructors to
handle different circumstances.
public Book ()
{
ReaderName = Nicole;
}
------------------------------------------public Book (string name)
{
ReaderName = name;
}
Concepts / Vocabulary
• Overloading Constructors:
• C# provides a default constructor even
if you do not provide your own.
• That being said, you can overload any
constructor and you can write as many
constructors as you want—however,
beware redundancy.
Module Ten
Object Initializers
Concepts / Vocabulary
• Initializers:
• Gives the programmer the ability to assign vales to
object properties (which in return their fields)
without calling the constructor, or passing a
parameter.
• *Requires a default constructor within the class.
• Example:
Student aStudent = new Student (StudentAge = 25);
Concepts / Vocabulary
• Benefits:
• Allows you to create multiple objects with
different initial assignments.
• This means you do not need to worry about
overloading multiple constructors.
• Example:
• Class Student
{
public int Age { get; set; }
• It also allows you to create objects with different
starting values.
public string Name { get; set; }
public int Id { get; set; }
public Student()
{
Age = 25;
Name = Nicole;
Id = 2130498;
}
}
Module Ten
Overloading Operators
Concepts / Vocabulary
• Overloading Operators:
• Allows you to use arithmetic symbols
with an object.
• Unary Operators:
• +, -, !, ++, --, true, false
• Binary Operators:
• +, -, *, /, %, &, |, ==, !=, >, =,
Purchase answer to see full
attachment