Anda di halaman 1dari 6

Classes and members and access modifiers

Access Modifiers

An access modifier identifies and specifies the level of encapsulation associated with every class
member in other words, in a way it sets the level of visibility and openness to modification to the
outside world.

There are 5 levels of access modifiers

1. Public
Is the most permissive one, everything that is public is visible and it can be changed, and in
some cases it breaks encapsulation because the devs often expose things that shouldn’t be
exposes

2. Private
Is the most restrictive. Everything that is private is accessible only from inside that class, and not
available to the outside world.
The default access modifier of the members of a class is private, and everytime there is no
access modifier specified, that member is private.
3. Protected
4. Internal
5. Protected internal
The default access modifier of a class is internal.

Static
To access a member that is static there is no need to create an instance of a class.

Only one class that is static is available in your app with only one part of memory for it.

You can’t access non-static member from inside a static member.

You can have static properties, fields, and methods.

Classes
The most fundamental construct in object-oriented programming is the class. A group of classes form a
programming abstraction, model, or template of what is often a real-world concept.
Classes exhibit the three principal characteristics of object-oriented programming: encapsulation,
inheritance, and polymorphism.

A class is a template for what an object will look like. It’s like a mold that the cooks use to cook
cupcakes. They use special trays that give a special form.

The process of creating an object from a class is called instantiation, and the ‘thing’ created is called
instance, or object.

Once you have defined a new class, you can use that class as though it were built into the
framework. In other words, you can declare a variable of that type or define a method that takes a
parameter of the new class type.

class Program
{
static void Main()
{
Employee employee1 = new Employee();
Employee employee2;
employee2 = new Employee();
}
}

Class members

Fields
When we define classes we need to declare members to hold the class state.
These variables, declared at the class level are called fields and store data associated with an
object, defining it’s state.

Example:
public class Employee
{
public string firstName;
public string lastName;
public int salary;
}

You can access an instance field only from an instance of the containing class (an object), and for that
you need to create an object.

The values of these fields can be given at declaration type, and will be the default values of the fields, or
can be set through the constructor.

Example:
public class Employee
{
public string firstName;
public string lastName;
public int salary=20;//has a default value
}

Constructor
After you added fields in a class to store data(state), you need to ensure that the data you store in the
class is valid.

IF you would want to create an object of type Employee using new operator you will be able to do that
because the compiler generates a default, parameterless constructor for you, behind the scenes even if
you don’t provide one.

When you create an object you need to ensure that your object is in a valid state, and it has all the
information needed.
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}

A constructor is like a method that doesn’t have a return type, the name is identical with the class name
and it’s invoked every time you instantiate an object of that type.
It can be overloaded, exactly like normal method, but his purpose is to ensure your class is in a valid
state.
public Employee(int id)
{
Id = id;
// Look up employee name...
// ...
}

Through a constructor, you pass data from external classes, and usually you assign that data, on the
fields and property inside your class.

If you have multiple constructors in your class these can be chained. Chaining, allows you to control
which constructor is called, and in which order.
class Employee
{
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}

public Employee(int id, string firstName, string lastName )


: this(firstName, lastName)
{
Id = id;
}

this keyword refers to the current instance of the class, and you will see it used often in projects.

Basically this, says that :”ok call this constructor that has two parameters, firstName, lastName, in the
current class”

Properties

Read-Write properties

public int Height


{
get
{
return this.height + 2;

}
set
{
if (value <= 5)
{
throw new ArgumentException("The height must me grater than 5");
}
else
{
height = value;
}

}
}

Write only properties


Sometimes you will need to have a property that allows only to write but not to read. In this case your
property will be write-only.

Read-only properties
You will often need properties that allow only reading but not writing. And int this case the setter part of
the property can be removed, and you will return values from another property or maybe a field.

Example
public string YearOfExpiration
{
get
{
return ExpirationDate.Year.ToString();
}
}

You will need to return only the year of expiration for a certain product, as a string , to be able
to display it somewhere in your program.

In this case, you don’t need to write the expiration date, because that will be calculated
automatically based on the production date.
public DateTime ExpirationDate
{
get
{
return ProductionDate.AddYears(3);
}
}

Automatic properties

An automatic property is a property that has no backing field in the class, and you don’t need to return a
certain field. At compile time, the compiler generates fields, but you are not able to access them.

public Guid Id { get; set; }


Guidelines:

Use automatic properties when you don’t need a field in the class, and the one generated by the
compiler is enough.

Use automatic properties when you don’t have validations to do in your properties, or you don’t need to
expose or set a certain field, or to do calculations automaticaly.

Enums
An enumeration specifies a set of named constants

Why use them?

Enumerations provide all of the advantages that constants provide and the following additional benefits:
• Code is easier to maintain because you assign only anticipated values to your variables.
• Code is easier to read because you assign easily identifiable names to your values.
• Code is easier to type because Microsoft IntelliSense® displays a list of the possible values that you can
use.
• Code is well formed because you can specify a set of constant values and define a type that will accept
values from only that set.

You can define enumerations in namespaces and classes but not in methods.

Enumerations values start at 0 by default, unless specified otherwise.

enum Seasons { Spring, Summer, Fall, Winter};

Internally, an enumeration type associates an integer value with each element of the
enumeration. By default, the numbering starts at 0 for the first element and increments in steps of 1. If
you prefer, you can associate a specific integer constant (such as 1) with an enumeration literal (such as
Spring), as in the following code example. In this case, the enumeration literals Summer, Fall, and
Winter automatically have the values 2, 3, and 4.

enum Season { Spring = 1, Summer, Fall, Winter=5 }

You can explicitly cast enums to int to obtain the corresponding values.

int dayNumber =(int) WeekDays.Thursday;

Anda mungkin juga menyukai