How do I get the MemberNames property with the index?

-4

I started programming in C # a short time ago and I'm doing a validation treatment using Data Annotation with windows forms, I'm having a problem that I can not get the MemberNames property of the IEnumerable Interface by index, how can I get the property of index 0, that is, the Name property.

    
asked by anonymous 25.08.2018 / 05:07

1 answer

0

Since MemberNames is a string[] and you want to retrieve the first value of it you can explicitly use index 0 as long as you verify that the array has in fact been initialized. Another option is the FirstOrDefault() method, adding the reference of System.Linq .

var _obj = "";
var memberNames = error.MemberNames;

//Pelo índice
_obj = (memberNames != null) ? memberNames[0] : "";

//Usando Linq
_obj = memberNames?.FirstOrDefault() ?? "";
  

After you edit your question, with your code being the class you are validating, I edit the question with a more complete answer.

    
25.08.2018 / 14:16