Jump to content

c# struct vs class, c# programming question


mickle026

Recommended Posts

mickle026

Hi,

 

Im hoping someone can help.  Im trying to understand how to initialise and use a list in a Struct (if it is possible)

(I can do it in a class, im trying to fathom why I cannot do it in a struct, if its possible what im a doing wrong)

public struct Person
{
  public string Name;
  public List<string> Weblinks;
  
  public Person(string name,List<string> weblinks)
  {
    Name = name;
    Weblinks = weblinks;
  }
}

So when I try to add to the list

Person NewPerson = new Person();

NewPerson.Weblinks.Add("www.somelink.htm");

ima am getting

Quote

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Now im maybe understanding this incorrectly, but i am understanding that I do not have an initialised list to add to.

So I tried to create the list in the constructor and get the same problem

public struct Person
{
  public string Name;
  public List<string> Weblinks;
  
  public Person(string name,List<string> weblinks)
  {
    Name = name;
    weblinks = new List<string>();
    Weblinks = weblinks;
  }
}
Quote

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Same as this error in this image here:

Untitled.jpg.9b7408c7a4059eb528b467307d46bbee.jpg

Does anyone know how to do this is a struct ?

Many thanks

Link to comment
Share on other sites

You can't. Generally speaking for your emby plugin you'll pretty much never be creating structs.

Link to comment
Share on other sites

mickle026

Thanks for the info,

Im using a class for my code because of the need for a list, but its for a processing data function that doesnt communicate with emby itself.

I just wanted to know if it were possible in the struct because the struct seems to be way faster than the class when itterating through lots of data.

 

Now i have a lot more understanding in this area (still not perfect) its helping make my code much better , much shorter and faster as well :)

 

But thank you.

Edited by mickle026
Link to comment
Share on other sites

Quote

I just wanted to know if it were possible in the struct because the struct seems to be way faster than the class when itterating through lots of data.

Not to go off topic but I can assure that this isn't true. The real determining factor when iterating over a list is the actual work that you're doing as pass over each one.

  • Thanks 1
Link to comment
Share on other sites

TeamB
5 hours ago, mickle026 said:
Person NewPerson = new Person();

NewPerson.Weblinks.Add("www.somelink.htm");

your calling the default constructor () not the constructor with the params so your class level instance objects are still null.

sorry just saw that you were asking about structs, there is some info on struct constructors here

https://www.tutorialsteacher.com/csharp/csharp-struct

Edited by TeamB
Link to comment
Share on other sites

TeamB
2 hours ago, Luke said:

Not to go off topic but I can assure that this isn't true. The real determining factor when iterating over a list is the actual work that you're doing as pass over each one.

@mickle026 yes this, in most languages including c# class and struct are handled nearly exactly the same once they are constructed. How you iterate over the data is the key point.

  • Agree 1
  • Thanks 1
Link to comment
Share on other sites

Cheesegeezer
15 hours ago, mickle026 said:

Thanks Guys :)

A little table for the differences.  I never ever use Structs

image.png.af6fa2e174d05cb020b6b2c988fc00ae.png

  • Thanks 1
Link to comment
Share on other sites

mickle026

Thanks again guys.

I feel that my knowledge has seriously stepped up a level since I'm starting to understand this and now even trying to understand the differences.

Some of my amateur code has massively shrunk from 100's of lines to quite simply just a few.  To me that's just - wow!

That by Itself speaks volumes, but also the speed has gone from cumbersome average to fast, and that alone has made me feel more confident that I'm understanding this much better.

All the help is very much appreciated 👍

 

Link to comment
Share on other sites

TeamB
7 hours ago, Cheesegeezer said:

A little table for the differences.  I never ever use Structs

image.png.af6fa2e174d05cb020b6b2c988fc00ae.png

can you post the source for this

Link to comment
Share on other sites

BillOatman
On 10/8/2022 at 4:36 PM, TeamB said:

can you post the source for this

Formatted differently, but content looks the same here

This seems to explain it pretty well to me.

Quote

As a rule of thumb, the majority of types in a framework should be classes. There are, however, some situations in which the characteristics of a value type make it more appropriate to use structs.

✔️ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

❌ AVOID defining a struct unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (int, double, etc.).

  • It has an instance size under 16 bytes.

  • It is immutable.

  • It will not have to be boxed frequently.

In all other cases, you should define your types as classes.

 

Edited by BillOatman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...