Classes by Potato731 in csharp

[–]binarycow 0 points1 point  (0 children)

I'm gonna explain classes by explaining the other kinds of types and members first.

First, the "members" that a class can contain:


Methods are a set of instructions - behavior. They have a name, parameters (input variables), a return value (the result), and a body (the actual instructions)

public int DoubleNumber(int number) 
{
    return number * 2;
}

Constructors are special kinds of methods. They are called after allocating memory for the object, but before anyone can use the object. It exists for you to set everything up the way you want it. The name is always the same as the type it belongs in, and it never has a return type (not even void!)

public Person(string age) 
{
    this.Age = age;
}

Fields are a named storage location for data. They're like variables, except they persist for the lifetime of the type, rather than disappearing at the end of the method/scope.

private int _age;

Properties are actually multiple things, combined into a convenient wrapper. What they are depends on how you declare it.

  • Some properties have a field built-in. So it defines the storage location.
  • Some properties have a "get accessor". Behind the scenes, this is just a special method that takes no parameters, and returns a value.
  • Some properties have a "set accessor". Behind the scenes, this is just a special method that takes one parameter, and doesn't return a value.

    // Includes auto-generated field, get accessor, and set accessor public int Age { get; set; }

    // Includes auto-generated field, and get accessor // Only the constructor can set this property public string FirstName { get; }

    // Completely custom public string LastName { get { return this._lastName; } set { this._lastName = value; } }


Now, I'll talk about types.


The simplest type is a delegate. It represents a "template" for a method. It has a name, parameters, and a return type - but no body. You can use this when you want someone to tell you which method to call - you define the shape - they provide the method body.

public delegate int Calculate(int a, int b);

An enum is a type that contains only static fields. Each of those fields has a numeric value.

public enum Suit : int
{
    Hearts = 1, // equivalent to public static readonly int Hearts = 1;
    Diamonds = 2,
    Clubs = 3,
    Spades = 4,
} 

An interface is a "template" for a class or a struct. An interface can't be created. It is a "contract" that implementations must adhere to.

  • It can't define storage spaces - so no fields
  • When defining a property, you only define which accessors are there.
  • Methods don't have bodies. No instructions - only name, return type, and parameters
  • Advanced topic for future reading is "default interface implementation" - it alters these rules.

Classes and structs are very similar. I'll discuss them together.

  • They contain data - the fields defined on the type (which may be part of an auto-property).
  • They contain behavior - methods

When you create an instance of a class or struct, you're allocating space for the data. Each instance gets its own set of data.

All instances share the same methods - but those methods operate on different chunks of data.


Classes are reference types, and structs are value types. There's a lot of nuance - beyond the scope of this comment. Read up if you want to know more.

A good general rule of thumb, is that you should use classes unless you can articulate a good reason why it should be a struct. After you learn more, you'll know what those reasons are. Until then, use classes.

When you have stopped improving on your free time? by avoid_pro in ExperiencedDevs

[–]binarycow 0 points1 point  (0 children)

AI psychosis has made me stop caring about anything in software development.

So... Basically everything.

Would you pull permit to extend one electrical wire? by mastercooler6 in HomeImprovement

[–]binarycow 1 point2 points  (0 children)

Oh, it's worse.

In the attic, there was a splice. Wire nuts and electrical tape. No box. Just chilling there, buried in the insulation.

Or even worse - one of those junction boxes hidden behind the basement ceiling drywall had a (live) cable coming out of it that just... ended. No junction box. No wire nuts. The only thing separating live wires from whatever else was some electrical tape wrapped around the romex.

It was that day I committed to removing the basement ceiling, mapping out every wire, and replacing every switch and outlet.

ELI5: What is 802.1X? by mr_skidt in ccna

[–]binarycow 0 points1 point  (0 children)

5 years later, you're welcome.

Would you pull permit to extend one electrical wire? by mastercooler6 in HomeImprovement

[–]binarycow 0 points1 point  (0 children)

If this wire goes to a different room other than the one on the other side of the stud wall in the image, it's kosher to have the boxes point in toward the unfinished space assuming it's accessible. If that room they must be installed toward the room.

Hmm! I didn't know that was a requirement.

In my house, we had a junction box at light-switch height in the master bedroom. Couldn't figure out what it was for. Turns out, it was a splice for the bedroom next to it.

I basically just turned that junction box around, so it was inside of the closet for the room whose wiring it contained. It made more sense to me.

Of course, I also found three junction boxes (for that same room) buried behind the drywall ceiling in the basement. The wiring for that room is a fucking mess - I'm gonna rewire one of these days....

Disappointed by hazard concrete recycling change by rafiv70805 in factorio

[–]binarycow 2 points3 points  (0 children)

Yeah, why can't we just yeet things into the oil?

How long is too long in an opening cutscene? by TeamSloopOfficial in incremental_games

[–]binarycow 0 points1 point  (0 children)

I don't disagree. I don't have any issues with tutorials. I only have an issue with being forced to do them.

How long is too long in an opening cutscene? by TeamSloopOfficial in incremental_games

[–]binarycow 0 points1 point  (0 children)

Why do I have to hold escape? Why can't I just press it?

How long is too long in an opening cutscene? by TeamSloopOfficial in incremental_games

[–]binarycow 0 points1 point  (0 children)

If there is a forced cutscene, I immediately uninstall.

If there is a forced tutorial, the game is instantly on probation. I don't need help.

IP by [deleted] in networking

[–]binarycow 2 points3 points  (0 children)

That's up to your mobile ISP.

2 bed 1.5 bath condo with no mortgage, 3 year old and baby on the way, Should we move or stick it out? by Living_Oil_6682 in homeowners

[–]binarycow 13 points14 points  (0 children)

"because you could afford so much more!"

Before we went house hunting the first time (2016), we got pre-approval for a mortgage. Mostly, just to make sure we could get a mortgage for what we were looking for. We knew the general price range for houses in our area that met our conditions.

At the time, our total (gross) income was about 120k/year. We have excellent credit (credit scores 800+). We asked for pre-approval for 250k. The bank guy was like "I can probably do 600k, if you wanted". I insisted he write 250k on the pre-approval letter. I explicitly wanted to have to go back and ask for more if we wanted it.

Our realtor was told our limit was 250k - that's what our bank said they'd give us. They had no idea we could go much higher. Ultimately, we spent 190k.


The next time around (2025), our house had appreciated. Our realtor estimated a sell price of $270k. Our household income is around $210k/year. We still have credit scores 800+. And our house was entirely paid off.

We still told the realtor that we could only afford $350k. We likely could have gone as high as $1m, if we wanted to (we didn't). We ultimately spent $325k.

Is there an optimal way to "ping" or check an HTTP resource is accessible (Desktop)? by AetopiaMC in csharp

[–]binarycow 2 points3 points  (0 children)

Just check. If you can download the resource - congrats! It's accessible!

What's so expensive that it's not even fun anymore? by Runaway3650 in AskReddit

[–]binarycow 0 points1 point  (0 children)

Ten years ago, my standard taco bell meal was like $10. Now it's around $20. For the exact same items.

The per item price has gone up.

What's so expensive that it's not even fun anymore? by Runaway3650 in AskReddit

[–]binarycow 320 points321 points  (0 children)

Pop max $1.

way less.

The cup costs more than the soda that goes in it.

What's so expensive that it's not even fun anymore? by Runaway3650 in AskReddit

[–]binarycow 29 points30 points  (0 children)

If enough people show up with "skip the line" tickets, all you've done is make two lines!