Should `Phone` be a class or a struct in C#? [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a Phone type with two properties: Number and Description (work, home, cell, etc.). I do not know if it should be a class or a struct.
According to MSDN :
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.
I am certain it will not be more than 16 bytes, but I am not sure it will meet the other requirements. So which should I use?

When in doubt, use a class. It works better in most cases anyway. structs are for special cases - you'll know when you hit one.

Related

Store 2^256 big integer value in variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Is there a data type to store 2^256 big integer numbers in variables in C#? As far as I know it exceeds the limit of BigInt.
Assuming that you're talking about exact integers: BigInteger is arbitrarily large. However, it may also be possible to just use a quad of Int64, depending on the exact operations you need to perform.

IArithmetic<T> interface in c# [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I explored c# source code reference. And I came across with interesting mention of IArithmetic<T> interface. For example, Int32, Double contain commented implementations of IArithmetic interface. I am interested by these details. As I understood, it is attempt to add "supporting" of arithmetic operations. But why are they commented? Is it bad way to add supporting generic "operators"?
It was probably scrapped due to performance reasons and not very much usability.
Primitive types supporting arithmetic operations through an interface is really not a very attractive scenario; performance would be horrible compared to simply using the value type itself due to the necessary boxing and unboxing.
What possible uses? Well, the first one to spring to mind would be the following scenario:
public Matrix<T> where T: IArithmetic<T>
or some such. Although this could be interesting, due to performance reasons, it would probably need to be solved some other way, not through interfaces; read this for very educated musing on the subject.
On top of all that, if you really need something similar to Arithmetic<T> you can always build your own with an added level of indirection.

Default parameter value is good practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a controller method called GetCustomer(int id=0)
This smells weird in my opinion, but was already here, and I'm wondering if this is a good practice!
I would rather in this case use nullable type!
Assuming that action maps onto GET /customers/id, making the id optional (by means of a parameter with a default value or a nullable type) is bad API design.
Traditionally, GET /customers maps onto an action that retrieves all existing customers, not onto GET /customers/0. You can also simply not support calls to GET /customers.

What is the best use of a variable when required by multiple methods [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm using XmlManager to
do xml manipulations in several methods in a class.where I should declare XmlManager variable ?
1.locally within each method and do intialization.
2 declare at globally and initiate at the method level
As it is, in this question, there's absolutely NO difference whatsoever because there's neither performance gain nor significant design issues.
Maybe if the question is put into context there could be reason to choose one approach over the other, but as it stands now. None of the approach is better than the other one

List of structs vs classes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a relatively small dictionary (a few hundred entries at most) that is receiving many calls (hundreds, possibly thousands per second) and many of them requires entry modification.
Performance wise, which one of this solution is generally recommended for a small list with frequent updates?
unboxing-boxing structs
define structure methods for each parameter that requires modification
use classes, that can be directly modified because they are referenced unlike structures
You should really avoid mutable value types (i.e. structures that can be modified) if at all possible, as it basically breaks the concept of a "value type" if one or more attributes of a value are not intrinsically part of the value itself (and thus can't be changed). If you need to store values that can be changed, then you should be using a class.

Categories

Resources