C# Class - too generic causing unnecessary overhead? The Next CEO of Stack OverflowCan a C# anonymous class implement an interface?How do I clone a generic list in C#?When to use static classes in C#Why does C# forbid generic attribute types?How can I return NULL from a generic method in C#?How to get the type of T from a member of a generic class or method?A generic list of anonymous classJava Generics Wildcarding With Multiple ClassesWhen is it appropriate to use C# partial classes?Generate C# class from XML
What's the best way to handle refactoring a big file?
Is it professional to write unrelated content in an almost-empty email?
How to avoid supervisors with prejudiced views?
Multiple labels for a single equation
Why has the US not been more assertive in confronting Russia in recent years?
What flight has the highest ratio of time difference to flight time?
Is there a way to save my career from absolute disaster?
If a black hole is created from light, can this black hole then move at speed of light?
How do I avoid eval and parse?
Written every which way
Anatomically Correct Strange Women In Ponds Distributing Swords
Non-deterministic sum of floats
Why didn't Khan get resurrected in the Genesis Explosion?
To not tell, not take, and not want
What benefits would be gained by using human laborers instead of drones in deep sea mining?
Rotate a column
Which tube will fit a -(700 x 25c) wheel?
How powerful is the invisibility granted by the Gloom Stalker ranger's Umbral Sight feature?
How do I make a variable always equal to the result of some calculations?
Skipping indices in a product
Complex fractions
Do I need to enable Dev Hub in my PROD Org?
How do scammers retract money, while you can’t?
In excess I'm lethal
C# Class - too generic causing unnecessary overhead?
The Next CEO of Stack OverflowCan a C# anonymous class implement an interface?How do I clone a generic list in C#?When to use static classes in C#Why does C# forbid generic attribute types?How can I return NULL from a generic method in C#?How to get the type of T from a member of a generic class or method?A generic list of anonymous classJava Generics Wildcarding With Multiple ClassesWhen is it appropriate to use C# partial classes?Generate C# class from XML
building using Webforms
I have a number of classes they contain all sorts of properties:
Lets say:
USER:
Forename,
Surname,
EyeColour,
FavouriteIceCream
In different parts of my site I need to display a list of users thus:
Bob Jones,
Fred Smith,
Susan Gifford
whereas in other parts I need a list:
GIFFORD, Susan
JONES, Bob
SMITH, Fred
Like wise I might need to abbreviate their names to
BJo
FSm
SGi
In my head it seems sensible to have additional properties inside my class called:
Fullname (forename + ' ' + surname)
Reverse (ToUpper(surname) +', ' + forename)
Intials
which are filled at time of creating the USER object and so later when I want to display Bob I can call ThisUser.FullName
rather than
ThisUser.Forename + " " + ThisUser.Surname
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Obviously I've simplified here but is it bad to have the FavouriteIceCream
field being substantiated every time I refer to a User even if I only use their favourite flavour on a page viewed infrequently.
To me it seems a good way to be consistent, any page that wants to use the reverse format of a name is always going to be JONES, Bob
rather than sometimes Jones, Bob
if the page is written by one of the team who has forgotten the format required
The other side of the argument I guess would that we have a bare minimum user class and we pull in the extra fields only when we need to know their dairy preferences
//additional,
if it makes a difference, internal intranet based webform so only 200 users
c# asp.net class webforms
add a comment |
building using Webforms
I have a number of classes they contain all sorts of properties:
Lets say:
USER:
Forename,
Surname,
EyeColour,
FavouriteIceCream
In different parts of my site I need to display a list of users thus:
Bob Jones,
Fred Smith,
Susan Gifford
whereas in other parts I need a list:
GIFFORD, Susan
JONES, Bob
SMITH, Fred
Like wise I might need to abbreviate their names to
BJo
FSm
SGi
In my head it seems sensible to have additional properties inside my class called:
Fullname (forename + ' ' + surname)
Reverse (ToUpper(surname) +', ' + forename)
Intials
which are filled at time of creating the USER object and so later when I want to display Bob I can call ThisUser.FullName
rather than
ThisUser.Forename + " " + ThisUser.Surname
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Obviously I've simplified here but is it bad to have the FavouriteIceCream
field being substantiated every time I refer to a User even if I only use their favourite flavour on a page viewed infrequently.
To me it seems a good way to be consistent, any page that wants to use the reverse format of a name is always going to be JONES, Bob
rather than sometimes Jones, Bob
if the page is written by one of the team who has forgotten the format required
The other side of the argument I guess would that we have a bare minimum user class and we pull in the extra fields only when we need to know their dairy preferences
//additional,
if it makes a difference, internal intranet based webform so only 200 users
c# asp.net class webforms
add a comment |
building using Webforms
I have a number of classes they contain all sorts of properties:
Lets say:
USER:
Forename,
Surname,
EyeColour,
FavouriteIceCream
In different parts of my site I need to display a list of users thus:
Bob Jones,
Fred Smith,
Susan Gifford
whereas in other parts I need a list:
GIFFORD, Susan
JONES, Bob
SMITH, Fred
Like wise I might need to abbreviate their names to
BJo
FSm
SGi
In my head it seems sensible to have additional properties inside my class called:
Fullname (forename + ' ' + surname)
Reverse (ToUpper(surname) +', ' + forename)
Intials
which are filled at time of creating the USER object and so later when I want to display Bob I can call ThisUser.FullName
rather than
ThisUser.Forename + " " + ThisUser.Surname
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Obviously I've simplified here but is it bad to have the FavouriteIceCream
field being substantiated every time I refer to a User even if I only use their favourite flavour on a page viewed infrequently.
To me it seems a good way to be consistent, any page that wants to use the reverse format of a name is always going to be JONES, Bob
rather than sometimes Jones, Bob
if the page is written by one of the team who has forgotten the format required
The other side of the argument I guess would that we have a bare minimum user class and we pull in the extra fields only when we need to know their dairy preferences
//additional,
if it makes a difference, internal intranet based webform so only 200 users
c# asp.net class webforms
building using Webforms
I have a number of classes they contain all sorts of properties:
Lets say:
USER:
Forename,
Surname,
EyeColour,
FavouriteIceCream
In different parts of my site I need to display a list of users thus:
Bob Jones,
Fred Smith,
Susan Gifford
whereas in other parts I need a list:
GIFFORD, Susan
JONES, Bob
SMITH, Fred
Like wise I might need to abbreviate their names to
BJo
FSm
SGi
In my head it seems sensible to have additional properties inside my class called:
Fullname (forename + ' ' + surname)
Reverse (ToUpper(surname) +', ' + forename)
Intials
which are filled at time of creating the USER object and so later when I want to display Bob I can call ThisUser.FullName
rather than
ThisUser.Forename + " " + ThisUser.Surname
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Obviously I've simplified here but is it bad to have the FavouriteIceCream
field being substantiated every time I refer to a User even if I only use their favourite flavour on a page viewed infrequently.
To me it seems a good way to be consistent, any page that wants to use the reverse format of a name is always going to be JONES, Bob
rather than sometimes Jones, Bob
if the page is written by one of the team who has forgotten the format required
The other side of the argument I guess would that we have a bare minimum user class and we pull in the extra fields only when we need to know their dairy preferences
//additional,
if it makes a difference, internal intranet based webform so only 200 users
c# asp.net class webforms
c# asp.net class webforms
asked Mar 8 at 14:23
cornishbobcornishbob
1
1
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Yes. Use read-only calculated properties:
public String Fullname
get
return Forename + " " + LastName;
add a comment |
I think that storing these properties’ values at the time the instance is created is a bad idea. But you still can use properties getters like this:
string Forename;
string Surname;
// set value for the private properties as usual
public User(string fn, string sn)
Forename = fn;
Surname = sn;
// getters
public string DefaultName
get return Forname + “ “ + Surname;
public string ReversedName
get return Surname + “ “ + Forename;
without taking extra space in memory for data that you just can manipulate at runtime. Otherwise, you can created methods for getting manipulated name, as the other answer suggested. Anyways, never storing redundant data is always best
add a comment |
which are filled at time of creating the USER object [...] have the field being substantiated every time I refer to a User...
Are you talking about creating static properties that you would populate? Why not just create dynamic properties? Something like this:
public string Fullname get return $"Forename Surname";
public string Reverse get return $"Surname.ToUpper(), Forename";
// etc.
There's no need to populate the same data twice, and these won't be used until you call them.
Basically it's not a choice between duplicating data vs. putting the logic all over the application, it's a choice between putting the logic all over the application or putting it on the object. Just put the logic on the object.
Yes, originally when creating the object from DB I'd have done Forename = dbReader["forename"].ToString(); Surname= dbReader["surname"].ToString(); Fullname = dbReader["forename"].ToString() + " " + dbReader["surname"].ToString(); thanks to this thread I will no longer be doing this!!
– cornishbob
Mar 8 at 14:47
add a comment |
As usual: it depends.
Filling a bunch of properties of a class to hold various versions of the same informations is IMHO not the best idea.
Depending on how complex this creating is and on how many places it will be used, this could be either just an extension method, a private method within the view that needs that specific kind of view or a direct property of the class itself.
By using a property of the class itself you have at least four possibilities:
Fill up while creating the object (if it is immutable)
public class Person
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
FullName = $"FirstName LastName";
public string FirstName get;
public string LastName get;
public string FullName get;Fill it up when the depending setter was called (if it is mutable)
public class Person
private string _firstName;
private string _lastName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName get; private set;
private void UpdateFullName()
FullName = $"FirstName LastName";
Create the value on the fly whenever it will be called (multiple times if called multiple times)
public class Person
public string FirstName get; set;
public string LastName get; set;
public string FullName => $"FirstName LastName";Create the value once when it is called and return this result until the depending value has been changed.
public class Person
private string _firstName;
private string _lastName;
private Lazy<string> _fullName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName => _fullName.Value;
private void UpdateFullName()
_fullName = new Lazy<string>(()=> $"FirstName LastName");
Especially the usage of Lazy<>
can help in the case of complex creation that is only used in certain places. It will compute the value once on the first call and then return the computed result on all consecutive calls. If the value should be re-evaluated (on the next call to it) you simply instantiate a new version of the Lazy<>
instance.
If you would combine the Lazy<>
approach with immutability you could also get something like this (which would be my pick, if the constraints allow it):
public class Person
private Lazy<string> _fullname;
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
_fullname = new Lazy<string>($"FirstName LastName");
public string FirstName get;
public string LastName get;
public string FullName => _fullname.Value;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065172%2fc-sharp-class-too-generic-causing-unnecessary-overhead%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Yes. Use read-only calculated properties:
public String Fullname
get
return Forename + " " + LastName;
add a comment |
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Yes. Use read-only calculated properties:
public String Fullname
get
return Forename + " " + LastName;
add a comment |
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Yes. Use read-only calculated properties:
public String Fullname
get
return Forename + " " + LastName;
Is it bad practice to be creating extra properties at time of creation if I'm not going to be using them often?
Yes. Use read-only calculated properties:
public String Fullname
get
return Forename + " " + LastName;
answered Mar 8 at 14:29
GBrandtGBrandt
54710
54710
add a comment |
add a comment |
I think that storing these properties’ values at the time the instance is created is a bad idea. But you still can use properties getters like this:
string Forename;
string Surname;
// set value for the private properties as usual
public User(string fn, string sn)
Forename = fn;
Surname = sn;
// getters
public string DefaultName
get return Forname + “ “ + Surname;
public string ReversedName
get return Surname + “ “ + Forename;
without taking extra space in memory for data that you just can manipulate at runtime. Otherwise, you can created methods for getting manipulated name, as the other answer suggested. Anyways, never storing redundant data is always best
add a comment |
I think that storing these properties’ values at the time the instance is created is a bad idea. But you still can use properties getters like this:
string Forename;
string Surname;
// set value for the private properties as usual
public User(string fn, string sn)
Forename = fn;
Surname = sn;
// getters
public string DefaultName
get return Forname + “ “ + Surname;
public string ReversedName
get return Surname + “ “ + Forename;
without taking extra space in memory for data that you just can manipulate at runtime. Otherwise, you can created methods for getting manipulated name, as the other answer suggested. Anyways, never storing redundant data is always best
add a comment |
I think that storing these properties’ values at the time the instance is created is a bad idea. But you still can use properties getters like this:
string Forename;
string Surname;
// set value for the private properties as usual
public User(string fn, string sn)
Forename = fn;
Surname = sn;
// getters
public string DefaultName
get return Forname + “ “ + Surname;
public string ReversedName
get return Surname + “ “ + Forename;
without taking extra space in memory for data that you just can manipulate at runtime. Otherwise, you can created methods for getting manipulated name, as the other answer suggested. Anyways, never storing redundant data is always best
I think that storing these properties’ values at the time the instance is created is a bad idea. But you still can use properties getters like this:
string Forename;
string Surname;
// set value for the private properties as usual
public User(string fn, string sn)
Forename = fn;
Surname = sn;
// getters
public string DefaultName
get return Forname + “ “ + Surname;
public string ReversedName
get return Surname + “ “ + Forename;
without taking extra space in memory for data that you just can manipulate at runtime. Otherwise, you can created methods for getting manipulated name, as the other answer suggested. Anyways, never storing redundant data is always best
edited Mar 8 at 14:51
answered Mar 8 at 14:31
Davide VitaliDavide Vitali
700316
700316
add a comment |
add a comment |
which are filled at time of creating the USER object [...] have the field being substantiated every time I refer to a User...
Are you talking about creating static properties that you would populate? Why not just create dynamic properties? Something like this:
public string Fullname get return $"Forename Surname";
public string Reverse get return $"Surname.ToUpper(), Forename";
// etc.
There's no need to populate the same data twice, and these won't be used until you call them.
Basically it's not a choice between duplicating data vs. putting the logic all over the application, it's a choice between putting the logic all over the application or putting it on the object. Just put the logic on the object.
Yes, originally when creating the object from DB I'd have done Forename = dbReader["forename"].ToString(); Surname= dbReader["surname"].ToString(); Fullname = dbReader["forename"].ToString() + " " + dbReader["surname"].ToString(); thanks to this thread I will no longer be doing this!!
– cornishbob
Mar 8 at 14:47
add a comment |
which are filled at time of creating the USER object [...] have the field being substantiated every time I refer to a User...
Are you talking about creating static properties that you would populate? Why not just create dynamic properties? Something like this:
public string Fullname get return $"Forename Surname";
public string Reverse get return $"Surname.ToUpper(), Forename";
// etc.
There's no need to populate the same data twice, and these won't be used until you call them.
Basically it's not a choice between duplicating data vs. putting the logic all over the application, it's a choice between putting the logic all over the application or putting it on the object. Just put the logic on the object.
Yes, originally when creating the object from DB I'd have done Forename = dbReader["forename"].ToString(); Surname= dbReader["surname"].ToString(); Fullname = dbReader["forename"].ToString() + " " + dbReader["surname"].ToString(); thanks to this thread I will no longer be doing this!!
– cornishbob
Mar 8 at 14:47
add a comment |
which are filled at time of creating the USER object [...] have the field being substantiated every time I refer to a User...
Are you talking about creating static properties that you would populate? Why not just create dynamic properties? Something like this:
public string Fullname get return $"Forename Surname";
public string Reverse get return $"Surname.ToUpper(), Forename";
// etc.
There's no need to populate the same data twice, and these won't be used until you call them.
Basically it's not a choice between duplicating data vs. putting the logic all over the application, it's a choice between putting the logic all over the application or putting it on the object. Just put the logic on the object.
which are filled at time of creating the USER object [...] have the field being substantiated every time I refer to a User...
Are you talking about creating static properties that you would populate? Why not just create dynamic properties? Something like this:
public string Fullname get return $"Forename Surname";
public string Reverse get return $"Surname.ToUpper(), Forename";
// etc.
There's no need to populate the same data twice, and these won't be used until you call them.
Basically it's not a choice between duplicating data vs. putting the logic all over the application, it's a choice between putting the logic all over the application or putting it on the object. Just put the logic on the object.
answered Mar 8 at 14:28
DavidDavid
150k28146212
150k28146212
Yes, originally when creating the object from DB I'd have done Forename = dbReader["forename"].ToString(); Surname= dbReader["surname"].ToString(); Fullname = dbReader["forename"].ToString() + " " + dbReader["surname"].ToString(); thanks to this thread I will no longer be doing this!!
– cornishbob
Mar 8 at 14:47
add a comment |
Yes, originally when creating the object from DB I'd have done Forename = dbReader["forename"].ToString(); Surname= dbReader["surname"].ToString(); Fullname = dbReader["forename"].ToString() + " " + dbReader["surname"].ToString(); thanks to this thread I will no longer be doing this!!
– cornishbob
Mar 8 at 14:47
Yes, originally when creating the object from DB I'd have done Forename = dbReader["forename"].ToString(); Surname= dbReader["surname"].ToString(); Fullname = dbReader["forename"].ToString() + " " + dbReader["surname"].ToString(); thanks to this thread I will no longer be doing this!!
– cornishbob
Mar 8 at 14:47
Yes, originally when creating the object from DB I'd have done Forename = dbReader["forename"].ToString(); Surname= dbReader["surname"].ToString(); Fullname = dbReader["forename"].ToString() + " " + dbReader["surname"].ToString(); thanks to this thread I will no longer be doing this!!
– cornishbob
Mar 8 at 14:47
add a comment |
As usual: it depends.
Filling a bunch of properties of a class to hold various versions of the same informations is IMHO not the best idea.
Depending on how complex this creating is and on how many places it will be used, this could be either just an extension method, a private method within the view that needs that specific kind of view or a direct property of the class itself.
By using a property of the class itself you have at least four possibilities:
Fill up while creating the object (if it is immutable)
public class Person
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
FullName = $"FirstName LastName";
public string FirstName get;
public string LastName get;
public string FullName get;Fill it up when the depending setter was called (if it is mutable)
public class Person
private string _firstName;
private string _lastName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName get; private set;
private void UpdateFullName()
FullName = $"FirstName LastName";
Create the value on the fly whenever it will be called (multiple times if called multiple times)
public class Person
public string FirstName get; set;
public string LastName get; set;
public string FullName => $"FirstName LastName";Create the value once when it is called and return this result until the depending value has been changed.
public class Person
private string _firstName;
private string _lastName;
private Lazy<string> _fullName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName => _fullName.Value;
private void UpdateFullName()
_fullName = new Lazy<string>(()=> $"FirstName LastName");
Especially the usage of Lazy<>
can help in the case of complex creation that is only used in certain places. It will compute the value once on the first call and then return the computed result on all consecutive calls. If the value should be re-evaluated (on the next call to it) you simply instantiate a new version of the Lazy<>
instance.
If you would combine the Lazy<>
approach with immutability you could also get something like this (which would be my pick, if the constraints allow it):
public class Person
private Lazy<string> _fullname;
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
_fullname = new Lazy<string>($"FirstName LastName");
public string FirstName get;
public string LastName get;
public string FullName => _fullname.Value;
add a comment |
As usual: it depends.
Filling a bunch of properties of a class to hold various versions of the same informations is IMHO not the best idea.
Depending on how complex this creating is and on how many places it will be used, this could be either just an extension method, a private method within the view that needs that specific kind of view or a direct property of the class itself.
By using a property of the class itself you have at least four possibilities:
Fill up while creating the object (if it is immutable)
public class Person
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
FullName = $"FirstName LastName";
public string FirstName get;
public string LastName get;
public string FullName get;Fill it up when the depending setter was called (if it is mutable)
public class Person
private string _firstName;
private string _lastName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName get; private set;
private void UpdateFullName()
FullName = $"FirstName LastName";
Create the value on the fly whenever it will be called (multiple times if called multiple times)
public class Person
public string FirstName get; set;
public string LastName get; set;
public string FullName => $"FirstName LastName";Create the value once when it is called and return this result until the depending value has been changed.
public class Person
private string _firstName;
private string _lastName;
private Lazy<string> _fullName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName => _fullName.Value;
private void UpdateFullName()
_fullName = new Lazy<string>(()=> $"FirstName LastName");
Especially the usage of Lazy<>
can help in the case of complex creation that is only used in certain places. It will compute the value once on the first call and then return the computed result on all consecutive calls. If the value should be re-evaluated (on the next call to it) you simply instantiate a new version of the Lazy<>
instance.
If you would combine the Lazy<>
approach with immutability you could also get something like this (which would be my pick, if the constraints allow it):
public class Person
private Lazy<string> _fullname;
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
_fullname = new Lazy<string>($"FirstName LastName");
public string FirstName get;
public string LastName get;
public string FullName => _fullname.Value;
add a comment |
As usual: it depends.
Filling a bunch of properties of a class to hold various versions of the same informations is IMHO not the best idea.
Depending on how complex this creating is and on how many places it will be used, this could be either just an extension method, a private method within the view that needs that specific kind of view or a direct property of the class itself.
By using a property of the class itself you have at least four possibilities:
Fill up while creating the object (if it is immutable)
public class Person
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
FullName = $"FirstName LastName";
public string FirstName get;
public string LastName get;
public string FullName get;Fill it up when the depending setter was called (if it is mutable)
public class Person
private string _firstName;
private string _lastName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName get; private set;
private void UpdateFullName()
FullName = $"FirstName LastName";
Create the value on the fly whenever it will be called (multiple times if called multiple times)
public class Person
public string FirstName get; set;
public string LastName get; set;
public string FullName => $"FirstName LastName";Create the value once when it is called and return this result until the depending value has been changed.
public class Person
private string _firstName;
private string _lastName;
private Lazy<string> _fullName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName => _fullName.Value;
private void UpdateFullName()
_fullName = new Lazy<string>(()=> $"FirstName LastName");
Especially the usage of Lazy<>
can help in the case of complex creation that is only used in certain places. It will compute the value once on the first call and then return the computed result on all consecutive calls. If the value should be re-evaluated (on the next call to it) you simply instantiate a new version of the Lazy<>
instance.
If you would combine the Lazy<>
approach with immutability you could also get something like this (which would be my pick, if the constraints allow it):
public class Person
private Lazy<string> _fullname;
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
_fullname = new Lazy<string>($"FirstName LastName");
public string FirstName get;
public string LastName get;
public string FullName => _fullname.Value;
As usual: it depends.
Filling a bunch of properties of a class to hold various versions of the same informations is IMHO not the best idea.
Depending on how complex this creating is and on how many places it will be used, this could be either just an extension method, a private method within the view that needs that specific kind of view or a direct property of the class itself.
By using a property of the class itself you have at least four possibilities:
Fill up while creating the object (if it is immutable)
public class Person
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
FullName = $"FirstName LastName";
public string FirstName get;
public string LastName get;
public string FullName get;Fill it up when the depending setter was called (if it is mutable)
public class Person
private string _firstName;
private string _lastName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName get; private set;
private void UpdateFullName()
FullName = $"FirstName LastName";
Create the value on the fly whenever it will be called (multiple times if called multiple times)
public class Person
public string FirstName get; set;
public string LastName get; set;
public string FullName => $"FirstName LastName";Create the value once when it is called and return this result until the depending value has been changed.
public class Person
private string _firstName;
private string _lastName;
private Lazy<string> _fullName;
public string FirstName
get => _firstName;
set
_firstName = value;
UpdateFullName();
public string LastName
get => _lastName;
set
_lastName = value;
UpdateFullName();
public string FullName => _fullName.Value;
private void UpdateFullName()
_fullName = new Lazy<string>(()=> $"FirstName LastName");
Especially the usage of Lazy<>
can help in the case of complex creation that is only used in certain places. It will compute the value once on the first call and then return the computed result on all consecutive calls. If the value should be re-evaluated (on the next call to it) you simply instantiate a new version of the Lazy<>
instance.
If you would combine the Lazy<>
approach with immutability you could also get something like this (which would be my pick, if the constraints allow it):
public class Person
private Lazy<string> _fullname;
public Person(string firstName, string lastName)
FirstName = firstName;
LastName = lastName;
_fullname = new Lazy<string>($"FirstName LastName");
public string FirstName get;
public string LastName get;
public string FullName => _fullname.Value;
edited Mar 8 at 14:51
answered Mar 8 at 14:33
OliverOliver
33.4k773118
33.4k773118
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065172%2fc-sharp-class-too-generic-causing-unnecessary-overhead%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown