Recent comments in /f/explainlikeimfive

whereami8017 OP t1_ixhjmm0 wrote

Okay let me clarify a few things, I had looked at the rules before posting this and I did see and read rule 7. I then went to the search bar and searched for a post similar to this that would answer my question and didn't find any so I made this post. If I had seen a post that would've answered my question, I wouldn't of made this post. I might delete this post in the morning idk, I'll decide then. Sorry for violating rule 7, let's just all be nice to each other please.

−1

MissFortuneDaBes t1_ixh812k wrote

When you get stabbed, all you really feel in the moment is the attacker's fist hitting your body while the blade enters the flesh. It will feel like someone punched you on the back/shoulder/whatever. If a lung gets punctured, you will feel that something is wrong rather fast. But yeah, if nothing "important" is cut, the character in the scene might be confused at first, and as they turn around react frightened when they see the bloody knife.

2

yubathetuba t1_ixh3e9g wrote

I have some expertise here as a former paramedic who has been first on scene to several of these where witnesses saw the whole thing. 75% get pissed and fight back hard with little effect of being stabbed, still pretty ok when the medic shows up. 20% pretty badly hurt, fight back a little, eventually crumple mostly from fatigue rather than pain/debility, still pretty together when I show up. 5% severely injured, crumple nearly immediately due to blood loss or collapsed lung, dead or on the way to it when I show up.

7

Truen1ght t1_ixg95ul wrote

CLASS vs STRUCT

------------------------------

A class is a file that contains a bunch of operations it can perform, and a bunch of things it contains. The operations it can perform are called "methods" or "functions" (both are accurate, but maybe you've only heard one of the two terms. they are interchangeable).

By default, most things within a class have private scope, meaning only the class itself can perform operations listed within the class, or access/edit the things it contains.

A struct is nearly the same thing as a class, but by default everything is publicly scoped. That means that things outside of this class can perform the operations listed by the struct, or access/edit the things held by the struct.

So, truthfully, there are only 2 real differences between the struct and the class :

  1. using either the keyword "struct" or "class"
  2. the default scoping. public for a struct, private for a class.

So why are there two things? The struct came first from way back when, long before C# ever existed. The class came later, when people became better programmers and decided "yeah, maybe by default I should hide stuff from other structs/classes so that I'm less likely to accidentally change something I shouldn't". The struct is a holdover from original C.

​

CONSTRUCTOR and DESTRUCTOR

----------------------------------

A constructor is a special method that all classes and structs have. If you do not explicitly declare this method within your class or struct, there is a default constructor that is provided instead. You cannot have a class or struct without a constructor method, because then there is no way for your program to create that class or struct before you would try to use it.

The job of the constructor is solely for creating your class or struct.

The opposite of the constructor is the destructor. Like the constructor, your class or struct must have one, and if you don't declare it explicitly within your class/struct, a default one is provided for you.

The job of the destructor is to delete the class/struct you created earlier, so that the memory it used while your program was running doesn't stay allocated. You may have heard the term "memory leak", that is what a destructor is meant to prevent. If you "leak" enough memory, eventually your computer will become unresponsive because it has no available RAM to do things. The only way to resolve the problem in that case is to restart your computer, which automatically clears out everything in RAM, freeing it up for use.

​

Hopefully this explains it well enough for you.

2

Triabolical_ t1_ixg5ier wrote

>A Class is like a Struct, but it can perform functions. The functions are part of the data stored in that object. Classes have done other features that Structs don't, but basically, think of it as Struct with functions.

Structs in C# have functions.

The big difference is that structs have value semantics and classes have reference semantics (almost always with very rare exceptions)...

3

Triabolical_ t1_ixg5avj wrote

>I'm not a C# dev so while the rest may be close enough, Imma have to just have a go at describing structs: I'm pretty sure they just typed sets of properties... Like a class without methods... Like you might instead make a pie struct (because honestly, pies probably don't need methods, they don't DO a lot, and they're probably not dynamic or complex enough to need a constructor) so then you just make a "feast" class that has a bunch of food struct-typed properties with single eat method that takes the name of a food and subtracts from its quantity

Structs are value types, while classes are reference types.

Structs exist in C# so that you can easily implement types with value semantics - like a bignum class - and to do interop with windows system APIs that were written with C and therefore require a very specific memory layout.

Struct *mostly* operate like classes; both can have methods with them.

1

Triabolical_ t1_ixg4zlo wrote

class vs struct

Structs are designed for very limited scenarios.

For what are called "value types" - types that behave like numbers or strings. They behave in the ways that we expect numbers to behave.

When you want to use system functions that expect a very specific layout of data in memory. I Windows, this is typically system stuff that is written with C or C++.

Unless it's one of those cases, you shouldn't use structs. You should use classes.

method vs constructor

Consider this code (written from memory, so it might have some errors ):

class MyNumber
{
private _number;

public MyNumber (int number) // constructor
{
_number = number;
}

public void PrintNumber()
{
Console.WriteLine(_number);
}
}

This class stores and prints out a number.

When this class is used, we always want it to have a number associated with it rather than a default value. If we try to write this:

MyNumber number = new MyNumber();

the compiler will complain. We need to write:

MyNumber number = new MyNumber(1345);

That ensures that instances of the class are in a known state - that is why we want to use constructors.

In this example, PrintNumber() is a method. It's not special the way a constructor is.

Hope that helps.

​

​

​

Methods are chunks of code that are associated with a specific class. The important part of a method is that it has access to the private class variables associated with a method

2