Home > other >  Downgrade inline conditional from .net6 to .netframework 4.6.1
Downgrade inline conditional from .net6 to .netframework 4.6.1

Time:10-13

I have project written with .net6 core I'm trying to downgrade code to .NETframework 4.6.1 facing some issues with it, particularly conditional inline statement.

as I know in.NETframework conditional if usage like this

 var DecodedPayload = valid ? true : false

but in .net6 written like this with some additional part after statementز

 var DecodedPayload = valid ? true : false, IsValid = valid

UPDATED

I think some important hints was missing in my question, maybe usefull to explain, this workinig code from .net6 I forgot to point that conditionl statment evaluated inside the return scope, see the original code .

   private VerifiedDecodedDataModel<TNotificationData> GetVerifiedDecodedData<TNotificationData>(string signedPayload)
        {
            if (string.IsNullOrEmpty(signedPayload))
            {
                throw new ArgumentNullException("Signed Payload is null");
            }

            var splitParts = signedPayload.Split('.'); // JWS header, payload, and signature representations

            EnsurePartElements(splitParts);

            var valid = VerifyToken(signedPayload);
            
            var payload = splitParts[1];

            return new VerifiedDecodedDataModel<TNotificationData> 
            {
                DecodedPayload = valid ? DecodeFromBase64<TNotificationData>(payload) : default,
                IsValid = valid
            };
        }

what the purpose of this addition after comma?

Thanks

CodePudding user response:

Are you sure it didn't say bool DecodedPayload = valid ? true : false, IsValid = valid; ? in which case: it defines two booleans, calculates the first via the conditional, then assignes the second from valid. Akin to two statements:

bool DecodedPayload = valid ? true : false;
bool IsValid = valid;

(note that the two lines are functionally identical, though!)

Note that var can only declare and assign one local, where-as explicit types can declare (and optionally assign) as many as you like of that same type - comma separated.

Note also: any such change is not related to the framework version, but rather: the language/compiler version, and: var goes back almost all the way (it was added in C# 3 at the same time as .NET Framework 3.5), so... I doubt you need to change anything here.

CodePudding user response:

With the update:

return new VerifiedDecodedDataModel<TNotificationData> 
{
    DecodedPayload = valid ? DecodeFromBase64<TNotificationData>(payload) : default,
    IsValid = valid
};

this is object initializer syntax; the comma simply marks the end of each member assignment (there can optionally be a final redundant comma after the last assignment). Like var, this again is valid in C# 3 or above, which is the same timeframe as .NET Framework 3.5, so: it should not present any difficulty, however: it is equivalent to:

var tmp = new VerifiedDecodedDataModel<TNotificationData>();
tmp.DecodedPayload = valid ? DecodeFromBase64<TNotificationData>(payload) : default;
tmp.IsValid = valid;
return tmp;

Again, you should not need to change anything here. If it isn't working / compiling, you should post the exact message that you're seeing.

  • Related