Classes and Structs - C# Academy: Learn the Basics of C# (2014)

C# Academy: Learn the Basics of C# (2014)

Chapter 4. Classes and Structs

Next, you also have to learn about Classes and Structs which are both important parts of the program. These are both important elements of the .NET initiative that show behaviors and structure of data. These make every element in the program logical and coherent.

See, classes and structs work like blueprints that provide the foundation to objects that open on runtime. For example, if your class is Netflix Shows, the type would be either Netflix, or Shows.

Classes

Classes are reference types. This is because variables are created when you make the object of the class. This then means that there’d be reference memory for your classes. When you make changes to one class, everything under that class would soon follow and reflect the changes that have been made.

Structs

Meanwhile, Structs are Value Types. When you have structs, actual data is created for each struct—which means that one should not be reflecting the other.

Here’s a good example:

public class Netflix Shows

{

// private field

private January 2016 date;

// public field

public string day;

// Netflix Shows in January 2016

public Netflix Shows in January 2016

{

get

{

February 2016;

}

set

{

// Set reasonable boundaries for probable birth dates.

if (value.Year > January 2016&& value.Year <= February.2016)

{

date = value;

}

else

throw Netflix Shows in January to February 2016();

}

}

}

Other Important Terms

There are other essential terms that are related to classes and structs—and you’ll find them all below!

Encapsulation

The first principle or pillar of object-oriented programming is called Encapsulation. This specifies how each of the class and struct members are accessible, and how they are intended to be used from the outside. You can make use of assembly to hide malicious exploits or coding errors.

Fields

Fields are variable types that have been declared in the class or structs that are basically members of their containing type.

For example, there may be two static or instant fields for each type, and you can modify the values of each type, too. You should be able to use fields for variables that have been accessible through portability. These should also be provided through indexers, properties, and methods.

You should then be able to declare the fields through access levels in each block next to the name of the field.

For example:

public class Netflix Shows

{

// private field

private January 2016 date;

// public field

public string day;

// Netflix Shows in January 2016

public Netflix Shows in January 2016

{

get

{

February 2016;

}

set

{

// Set reasonable boundaries for probable birth dates.

if (value.Year > January 2016&& value.Year <= February.2016)

{

date = value;

}

else

throw Netflix Shows in January to February 2016();

}

}

// Public method also exposes date field safely.

// Example call: January.S2016("2016, 1");

public void SetDate(string dateString)

{

DateTime dt = Convert.ToDateTime(dateString);

//

if (dt.Year > January 2016 && dt.Year <= February 2016)

{

date = dt;

}

else

throw Netflix Shows in January to February 2016 ();

}

public TimeSpan GetTimeSpan(string dateString)

{

DateTime dt = Convert.ToDateTime(dateString);

if (dt != null && dt.Ticks < date.Ticks)

{

return date - dt;

}

else

throw Netflix Shows in January to February 2016 ();

}

If you want to access any of those objects, just type: object.fieldname. For example, class.netflixshows

Take note that these will all be initialized before any of the constructors are made.

Properties

Properties are members that allow objects on a program to be read, and to be flexible enough to fit in various operating systems. Sometimes, they are called accessors because they do help the objects become accessible.

Other essential facts about Properties include:

1. Get Properties.These are accessors that help return value of the property—even in various levels.

2. Properties do not implementset accessors.

3. Thevaluethat’s assigned by the accessor is called value.

For body definitions, you have to make use of the following:

public string Name => First + " " + Last;

Here’s a good example:

class SchoolSchedule

{

private double seconds;

public double Hours

{

get { return seconds / 2400; }

set { seconds = value * 2400; }

}

}

class Schedule

{

static void Main()

{

ClassSchedule t = new ClassSchedule();

// Assigning the Hours property causes the 'set' accessor to be called.

t.Hours = 24;

// Evaluating the Hours property causes the 'get' accessor to be called.

Class.Schedule.WriteLine("Time in hours: " + t.Hours);

}

}

// Output: Time in hours: 24

Constants

Meanwhile, Constants are known to compile time as immutable values because they do not change or ruin the program’s life. They are often declared as const, which are also known as modifiers.

System.Object is known as a primary constant. However, arrays, structs, and other classes are not considered const, because they are not Read Only files. You can then define constant variables for each of your programs. For example:

class Annual Calendar 1

{

const int months = 12;

const int weeks = 52;

const int days = 365;

const double daysPerWeek = (double) days / (double) weeks;

const double daysPerMonth = (double) days / (double) months;

}

You could then use those variables this way:

class Calendar2

{

const int months = 12, weeks = 52, days = 365;

}

Or you could also use it for this:

class Calendar1

{

public const int months = 12;

}

As you can see, the variables of Calendar are used for each of the examples—because they are constant to the project, and parts of the project would not work if they are not around.

Methods

Next up are Methods.They are known as code blocks that contain statements in each of them. Methods are needed to equalize arguments, especially when the instructions are executed in the middle of the context. In short, it’s like you’re going to edit what you have onscreen, and you do have to be meticulous about it.

Methods also bring forth Method Signatures. For example, if you want to declare whether something is public or private. In order for programs to work, everything has to be executed and segregated clearly, and methods will help you make sure of that.

As you can see, various codes also make use of parentheses (). These are called Method Parameters. If the parentheses are empty, it means that the method does not have any parameters in them.

Here are some examples:

public class Netflix Shows

{

// private field

private January 2016 date;

// public field

public string day;

// Netflix Shows in January 2016

public Netflix Shows in January 2016

{

get

{

February 2016;

}

set

{

// Set reasonable boundaries for probable birth dates.

if (value.Year > January 2016&& value.Year <= February.2016)

{

date = value;

}

else

throw Netflix Shows in January to February 2016();

}

}

// Public method also exposes date field safely.

// Example call: January.S2016("2016, 1");

public void SetDate(string dateString)

{

DateTime dt = Convert.ToDateTime(dateString);

//

if (dt.Year > January 2016 && dt.Year <= February 2016)

{

date = dt;

}

else

throw Netflix Shows in January to February 2016 ();

}

public TimeSpan GetTimeSpan(string dateString)

{

DateTime dt = Convert.ToDateTime(dateString);

if (dt != null && dt.Ticks < date.Ticks)

{

return date - dt;

}

else

throw Netflix Shows in January to February 2016 ();

}

}

See, there are some parentheses above that do not have anything inside the parentheses—which means they could not be considered as methods. Here’s another one:

class Annual Calendar 1

{

const int months = 12;

const int weeks = 52;

const int days = 365;

const double daysPerWeek = (double) days / (double) weeks;

const double daysPerMonth = (double) days / (double) months;

As you can see, there are variables inside the parentheses—which means that there are method parameters in the second example above.

Constructors

Constructors are the next string of variables that come after classes and structs. These enable default values in the program, together with codes and limit.

Take note that if you do not give any constructors to your objects, the program would create one for itself—but it might not be coherent with your program—so make sure that you input the values yourself.

Deconstructors

Of course, if there are constructors, there are also deconstructors. These cannot be defined in the structs, and each class could only have one deconstructor. They cannot be overloaded or inherited, and are automatically invoked.

For example:

class Dishwashing

{

~Dishwashing() // destructor

{

// dishwashing statements...

}

}

protected override void Finalize()

{

try

{

// dishwashing...

}

finally

{

base.Finalize();

}

}