Click here to Skip to main content
15,902,189 members
Home / Discussions / C#
   

C#

 
QuestionTaking C# to a whole new level of Evil Pin
Foothill23-Nov-16 9:16
professionalFoothill23-Nov-16 9:16 
AnswerRe: Taking C# to a whole new level of Evil Pin
Richard Deeming23-Nov-16 12:04
mveRichard Deeming23-Nov-16 12:04 
If the base class constructor is doing too much, or allocates a lot of memory, this could prevent that work from taking place if the argument isn't valid. If you move the throw into the Derived constructor, the base class constructor has already run by the time the argument is validated.

It would definitely be cleaner with the validation code extracted to a real method - possibly in a separate class, if it's the sort of thing you use a lot:
C#
public static class Requires
{
    public static string NotNullOrEmpty(string value, string parameterName)
    {
        if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException(parameterName);
        return value;
    }
}

public class Derived
{
    public Derived(string val) : base(Requires.NotNullOrEmpty(val, nameof(val)))
    {
    }
}

System.ArgumentNullException: Value cannot be null.
Parameter name: val
at TestingApp.Requires.NotNullOrEmpty(String value, String parameterName)
at TestingApp.Derived..ctor(String val)
at TestingApp.Program.Main(String[] args)


With C# 7, I believe you'll be able to do something like this:
C#
public class Derived
{
    public Derived(string value) : base(string.IsNullOrEmpty(val) ? throw new ArgumentNullException(nameof(val)) : val)
    {
    }
}

C# 7 Additions – Throw Expressions – Structured Sight[^]

I'm not sure if that makes it any more readable, though. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionHow to create alert in c# Pin
Member 308047023-Nov-16 1:20
Member 308047023-Nov-16 1:20 
SuggestionRe: How to create alert in c# Pin
Richard MacCutchan23-Nov-16 1:24
mveRichard MacCutchan23-Nov-16 1:24 
AnswerRe: How to create alert in c# Pin
Eddy Vluggen23-Nov-16 1:50
professionalEddy Vluggen23-Nov-16 1:50 
GeneralRe: How to create alert in c# Pin
Gerry Schmitz23-Nov-16 6:43
mveGerry Schmitz23-Nov-16 6:43 
GeneralRe: How to create alert in c# Pin
Eddy Vluggen23-Nov-16 7:52
professionalEddy Vluggen23-Nov-16 7:52 
GeneralRe: How to create alert in c# Pin
Gerry Schmitz23-Nov-16 8:48
mveGerry Schmitz23-Nov-16 8:48 
GeneralRe: How to create alert in c# Pin
Eddy Vluggen23-Nov-16 9:07
professionalEddy Vluggen23-Nov-16 9:07 
AnswerRe: How to create alert in c# Pin
Gerry Schmitz23-Nov-16 9:28
mveGerry Schmitz23-Nov-16 9:28 
QuestionRe: How to create alert in c# Pin
Member 308047023-Nov-16 17:14
Member 308047023-Nov-16 17:14 
AnswerRe: How to create alert in c# Pin
Gerry Schmitz23-Nov-16 17:42
mveGerry Schmitz23-Nov-16 17:42 
AnswerRe: How to create alert in c# Pin
Mycroft Holmes23-Nov-16 13:21
professionalMycroft Holmes23-Nov-16 13:21 
AnswerRe: How to create alert in c# Pin
Pete O'Hanlon23-Nov-16 21:18
mvePete O'Hanlon23-Nov-16 21:18 
GeneralRe: How to create alert in c# Pin
Gerry Schmitz24-Nov-16 6:20
mveGerry Schmitz24-Nov-16 6:20 
GeneralRe: How to create alert in c# Pin
Richard MacCutchan24-Nov-16 6:34
mveRichard MacCutchan24-Nov-16 6:34 
GeneralRe: How to create alert in c# Pin
Gerry Schmitz24-Nov-16 6:51
mveGerry Schmitz24-Nov-16 6:51 
GeneralRe: How to create alert in c# Pin
Richard MacCutchan24-Nov-16 7:38
mveRichard MacCutchan24-Nov-16 7:38 
GeneralRe: How to create alert in c# Pin
Eddy Vluggen24-Nov-16 23:46
professionalEddy Vluggen24-Nov-16 23:46 
GeneralRe: How to create alert in c# Pin
Gerry Schmitz25-Nov-16 0:17
mveGerry Schmitz25-Nov-16 0:17 
AnswerRe: How to create alert in c# Pin
Wendelius1-May-17 18:15
mentorWendelius1-May-17 18:15 
QuestionHow to use Wizard functionality in Windows App using C# ASP.NET Pin
Member 1225689022-Nov-16 23:38
Member 1225689022-Nov-16 23:38 
AnswerRe: How to use Wizard functionality in Windows App using C# ASP.NET Pin
Pete O'Hanlon23-Nov-16 0:17
mvePete O'Hanlon23-Nov-16 0:17 
GeneralRe: How to use Wizard functionality in Windows App using C# ASP.NET Pin
Member 1225689023-Nov-16 1:14
Member 1225689023-Nov-16 1:14 
GeneralRe: How to use Wizard functionality in Windows App using C# ASP.NET Pin
Pete O'Hanlon23-Nov-16 1:30
mvePete O'Hanlon23-Nov-16 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.