Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0bb818

Browse files
authored
Use string.IsNullOrEmpty and ArgumentException.ThrowIfNullOrEmpty in many more places (#85858)
1 parent 7f35658 commit c0bb818

File tree

150 files changed

+344
-479
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+344
-479
lines changed

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/MaskedTextProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2710,7 +2710,7 @@ public bool VerifyString(string input, out int testPosition, out MaskedTextResul
27102710
{
27112711
testPosition = 0;
27122712

2713-
if (input == null || input.Length == 0) // nothing to verify.
2713+
if (string.IsNullOrEmpty(input)) // nothing to verify.
27142714
{
27152715
resultHint = MaskedTextResultHint.NoEffect;
27162716
return true;

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/PropertyDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public virtual PropertyDescriptorCollection GetChildProperties(object? instance,
336336
protected Type? GetTypeFromName(
337337
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] string? typeName)
338338
{
339-
if (typeName == null || typeName.Length == 0)
339+
if (string.IsNullOrEmpty(typeName))
340340
{
341341
return null;
342342
}

src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/TypeDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ public static EventDescriptorCollection GetEvents(object component, Attribute[]?
11361136
name = component.Site.Name;
11371137
}
11381138

1139-
if (name == null || name.Length == 0)
1139+
if (string.IsNullOrEmpty(name))
11401140
{
11411141
int ci = System.Threading.Interlocked.Increment(ref s_collisionIndex) - 1;
11421142
name = ci.ToString(CultureInfo.InvariantCulture);

src/libraries/System.Data.Common/src/System/Data/Common/ObjectStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute?
359359
{ // this means type implements IXmlSerializable
360360
Type? type = null;
361361
string? typeName = xmlReader.GetAttribute(Keywords.MSD_INSTANCETYPE, Keywords.MSDNS);
362-
if (typeName == null || typeName.Length == 0)
362+
if (string.IsNullOrEmpty(typeName))
363363
{ // No CDT polumorphism
364364
string? xsdTypeName = xmlReader.GetAttribute(Keywords.TYPE, Keywords.XSINS); // this xsd type: Base type polymorphism
365365
if (null != xsdTypeName && xsdTypeName.Length > 0)

src/libraries/System.Data.Common/src/System/Data/DataError.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal void SetColumnError(DataColumn column, string? error)
3939
{
4040
Debug.Assert(column != null, "Invalid (null) argument");
4141
Debug.Assert(column.Table != null, "Invalid (loose) column");
42-
if (error == null || error.Length == 0)
42+
if (string.IsNullOrEmpty(error))
4343
{
4444
// remove error from the collection
4545
Clear(column);

src/libraries/System.Data.Common/src/System/Data/DataSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ public string DataSetName
748748
DataCommonEventSource.Log.Trace("<ds.DataSet.set_DataSetName|API> {0}, '{1}'", ObjectID, value);
749749
if (value != _dataSetName)
750750
{
751-
if (value == null || value.Length == 0)
751+
if (string.IsNullOrEmpty(value))
752752
{
753753
throw ExceptionBuilder.SetDataSetNameToEmpty();
754754
}

src/libraries/System.Data.Common/src/System/Data/SimpleType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ internal XmlNode ToNode(XmlDocument dc, Hashtable prefixes, bool inRemoting)
229229
{
230230
XmlElement typeNode = dc.CreateElement(Keywords.XSD_PREFIX, Keywords.XSD_SIMPLETYPE, Keywords.XSDNS);
231231

232-
if (_name != null && _name.Length != 0)
232+
if (!string.IsNullOrEmpty(_name))
233233
{
234234
// this is a global type
235235
typeNode.SetAttribute(Keywords.NAME, _name);

src/libraries/System.Data.Common/src/System/Data/XDRSchema.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal void LoadSchema(XmlElement schemaRoot, DataSet ds)
4141

4242
// Get Locale and CaseSensitive properties
4343

44-
if (_schemaName == null || _schemaName.Length == 0)
44+
if (string.IsNullOrEmpty(_schemaName))
4545
_schemaName = "NewDataSet";
4646

4747
ds.Namespace = _schemaUri;
@@ -85,7 +85,7 @@ internal void LoadSchema(XmlElement schemaRoot, DataSet ds)
8585
if (FEqualIdentity(node, Keywords.XDR_ELEMENT, Keywords.XDRNS) ||
8686
FEqualIdentity(node, Keywords.XDR_ATTRIBUTE, Keywords.XDRNS))
8787
{
88-
if (strType == null || strType.Length == 0)
88+
if (string.IsNullOrEmpty(strType))
8989
return null;
9090

9191
// Find an ELEMENTTYPE or ATTRIBUTETYPE with name=strType
@@ -133,7 +133,7 @@ internal static bool IsTextOnlyContent(XmlElement node)
133133
Debug.Assert(FEqualIdentity(node, Keywords.XDR_ELEMENTTYPE, Keywords.XDRNS), $"Invalid node type {node.LocalName}");
134134

135135
string value = node.GetAttribute(Keywords.CONTENT);
136-
if (value == null || value.Length == 0)
136+
if (string.IsNullOrEmpty(value))
137137
{
138138
string type = node.GetAttribute(Keywords.DT_TYPE, Keywords.DTNS);
139139
return !string.IsNullOrEmpty(type);
@@ -311,7 +311,7 @@ private static Type ParseDataType(string dt, string dtValues)
311311
}
312312

313313
NameType nt = FindNameType(strType);
314-
if (nt == s_enumerationNameType && (dtValues == null || dtValues.Length == 0))
314+
if (nt == s_enumerationNameType && string.IsNullOrEmpty(dtValues))
315315
throw ExceptionBuilder.MissingAttribute("type", Keywords.DT_VALUES);
316316
return nt.type;
317317
}
@@ -332,7 +332,7 @@ internal static string GetInstanceName(XmlElement node)
332332
else
333333
{
334334
instanceName = node.GetAttribute(Keywords.TYPE);
335-
if (instanceName == null || instanceName.Length == 0)
335+
if (string.IsNullOrEmpty(instanceName))
336336
throw ExceptionBuilder.MissingAttribute("Element", Keywords.TYPE);
337337
}
338338

@@ -400,7 +400,7 @@ internal void HandleColumn(XmlElement node, DataTable table)
400400

401401
strType = typeNode.GetAttribute(Keywords.DT_TYPE, Keywords.DTNS);
402402
strValues = typeNode.GetAttribute(Keywords.DT_VALUES, Keywords.DTNS);
403-
if (strType == null || strType.Length == 0)
403+
if (string.IsNullOrEmpty(strType))
404404
{
405405
strType = string.Empty;
406406
type = typeof(string);
@@ -480,7 +480,7 @@ internal void HandleColumn(XmlElement node, DataTable table)
480480
column.Namespace = targetNamespace;
481481

482482
table.Columns.Add(column);
483-
if (strDefault != null && strDefault.Length != 0)
483+
if (!string.IsNullOrEmpty(strDefault))
484484
try
485485
{
486486
column.DefaultValue = SqlConvert.ChangeTypeForXML(strDefault, type);

src/libraries/System.Data.Common/src/System/Data/XMLSchema.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal static bool FEqualIdentity(XmlNode? node, string name, string ns)
8080
internal static bool GetBooleanAttribute(XmlElement element, string attrName, string attrNS, bool defVal)
8181
{
8282
string value = element.GetAttribute(attrName, attrNS);
83-
if (value == null || value.Length == 0)
83+
if (string.IsNullOrEmpty(value))
8484
{
8585
return defVal;
8686
}
@@ -447,22 +447,22 @@ internal void HandleRelation(XmlElement node, bool fNested)
447447
}
448448

449449
parentName = node.GetAttribute(Keywords.MSD_PARENT, Keywords.MSDNS);
450-
if (parentName == null || parentName.Length == 0)
450+
if (string.IsNullOrEmpty(parentName))
451451
throw ExceptionBuilder.RelationParentNameMissing(strName);
452452
parentName = XmlConvert.DecodeName(parentName);
453453

454454
childName = node.GetAttribute(Keywords.MSD_CHILD, Keywords.MSDNS);
455-
if (childName == null || childName.Length == 0)
455+
if (string.IsNullOrEmpty(childName))
456456
throw ExceptionBuilder.RelationChildNameMissing(strName);
457457
childName = XmlConvert.DecodeName(childName);
458458

459459
value = node.GetAttribute(Keywords.MSD_PARENTKEY, Keywords.MSDNS);
460-
if (value == null || value.Length == 0)
460+
if (string.IsNullOrEmpty(value))
461461
throw ExceptionBuilder.RelationTableKeyMissing(strName);
462462

463463
parentNames = value.TrimEnd(null).Split(new char[] { Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP });
464464
value = node.GetAttribute(Keywords.MSD_CHILDKEY, Keywords.MSDNS);
465-
if (value == null || value.Length == 0)
465+
if (string.IsNullOrEmpty(value))
466466
throw ExceptionBuilder.RelationChildKeyMissing(strName);
467467

468468
childNames = value.TrimEnd(null).Split(new char[] { Keywords.MSD_KEYFIELDSEP, Keywords.MSD_KEYFIELDOLDSEP });
@@ -666,7 +666,7 @@ public void LoadSchema(XmlSchemaSet schemaSet, DataSet ds)
666666
{
667667
_schemaName = schemaRoot.Id;
668668

669-
if (_schemaName == null || _schemaName.Length == 0)
669+
if (string.IsNullOrEmpty(_schemaName))
670670
{
671671
_schemaName = "NewDataSet";
672672
}
@@ -1208,7 +1208,7 @@ internal static DataColumn[] BuildKey(XmlSchemaIdentityConstraint keyNode, DataT
12081208
internal static bool GetBooleanAttribute(XmlSchemaAnnotated element, string attrName, bool defVal)
12091209
{
12101210
string? value = GetMsdataAttribute(element, attrName);
1211-
if (value == null || value.Length == 0)
1211+
if (string.IsNullOrEmpty(value))
12121212
{
12131213
return defVal;
12141214
}
@@ -1227,7 +1227,7 @@ internal static bool GetBooleanAttribute(XmlSchemaAnnotated element, string attr
12271227
internal static string GetStringAttribute(XmlSchemaAnnotated element, string attrName, string defVal)
12281228
{
12291229
string? value = GetMsdataAttribute(element, attrName);
1230-
if (value == null || value.Length == 0)
1230+
if (string.IsNullOrEmpty(value))
12311231
{
12321232
return defVal;
12331233
}
@@ -1287,7 +1287,7 @@ internal void HandleKeyref(XmlSchemaKeyref keyref)
12871287
if (table == null)
12881288
return;
12891289

1290-
if (refer == null || refer.Length == 0)
1290+
if (string.IsNullOrEmpty(refer))
12911291
throw ExceptionBuilder.MissingRefer(name);
12921292

12931293
ConstraintTable? key = (ConstraintTable?)_constraintNodes![refer];
@@ -1321,7 +1321,7 @@ internal void HandleKeyref(XmlSchemaKeyref keyref)
13211321
{
13221322
string relName = XmlConvert.DecodeName(GetStringAttribute(keyref, Keywords.MSD_RELATIONNAME, keyref.Name!));
13231323

1324-
if (relName == null || relName.Length == 0)
1324+
if (string.IsNullOrEmpty(relName))
13251325
relName = name;
13261326

13271327
int iExisting = fKey[0].Table!.DataSet!.Relations.InternalIndexOf(relName);
@@ -1383,7 +1383,7 @@ internal void HandleConstraint(XmlSchemaIdentityConstraint keyNode)
13831383
string? name;
13841384

13851385
name = XmlConvert.DecodeName(keyNode.Name);
1386-
if (name == null || name.Length == 0)
1386+
if (string.IsNullOrEmpty(name))
13871387
throw ExceptionBuilder.MissingAttribute(Keywords.NAME);
13881388

13891389
if (_constraintNodes!.ContainsKey(name))
@@ -1930,10 +1930,10 @@ internal static bool IsXsdType(string name)
19301930
if (_typeNs == Keywords.XSDNS)
19311931
return null;
19321932
XmlSchemaAnnotated? typeNode;
1933-
if (_type == null || _type.Length == 0)
1933+
if (string.IsNullOrEmpty(_type))
19341934
{
19351935
_type = isAttr ? attr!.RefName.Name : el!.RefName.Name;
1936-
if (_type == null || _type.Length == 0)
1936+
if (string.IsNullOrEmpty(_type))
19371937
typeNode = isAttr ? attr!.SchemaType : el!.SchemaType;
19381938
else
19391939
typeNode = isAttr ? FindTypeNode((XmlSchemaAnnotated)_attributes![attr!.RefName]!) : FindTypeNode((XmlSchemaAnnotated)_elementsTable![el!.RefName]!);
@@ -1956,7 +1956,7 @@ internal void HandleSimpleTypeSimpleContentColumn(XmlSchemaSimpleType typeNode,
19561956
SimpleType? xsdType = null;
19571957

19581958
// if (typeNode.QualifiedName.Namespace != Keywords.XSDNS) { // this means UDSimpleType
1959-
if (typeNode.QualifiedName.Name != null && typeNode.QualifiedName.Name.Length != 0 && typeNode.QualifiedName.Namespace != Keywords.XSDNS)
1959+
if (!string.IsNullOrEmpty(typeNode.QualifiedName.Name) && typeNode.QualifiedName.Namespace != Keywords.XSDNS)
19601960
{ // this means UDSimpleType
19611961
xsdType = new SimpleType(typeNode);
19621962
strType = typeNode.QualifiedName.ToString(); // use qualified name
@@ -2212,7 +2212,7 @@ internal void HandleAttributeColumn(XmlSchemaAttribute attrib, DataTable table,
22122212
{
22132213
XmlSchemaSimpleType node = (typeNode as XmlSchemaSimpleType)!;
22142214
xsdType = new SimpleType(node);
2215-
if (node.QualifiedName.Name != null && node.QualifiedName.Name.Length != 0 && node.QualifiedName.Namespace != Keywords.XSDNS)
2215+
if (!string.IsNullOrEmpty(node.QualifiedName.Name) && node.QualifiedName.Namespace != Keywords.XSDNS)
22162216
{
22172217
// this means UDSimpleType
22182218
strType = node.QualifiedName.ToString(); // use qualified name
@@ -2376,7 +2376,7 @@ internal void HandleElementColumn(XmlSchemaElement elem, DataTable table, bool i
23762376
xsdType = new SimpleType(simpleTypeNode!);
23772377
// ((XmlSchemaSimpleType)typeNode).Name != null && ((XmlSchemaSimpleType)typeNode).Name.Length != 0 check is for annonymos simple type,
23782378
// it should be user defined Named simple type
2379-
if (((XmlSchemaSimpleType)typeNode).Name != null && ((XmlSchemaSimpleType)typeNode).Name!.Length != 0 && ((XmlSchemaSimpleType)typeNode).QualifiedName.Namespace != Keywords.XSDNS)
2379+
if (!string.IsNullOrEmpty(((XmlSchemaSimpleType)typeNode).Name) && ((XmlSchemaSimpleType)typeNode).QualifiedName.Namespace != Keywords.XSDNS)
23802380
{
23812381
strType = ((XmlSchemaSimpleType)typeNode).QualifiedName.ToString(); // use qualified name
23822382
type = ParseDataType(strType);
@@ -2582,13 +2582,13 @@ internal void HandleDataSet(XmlSchemaElement node, bool isNewDataSet)
25822582

25832583
// reuse variable
25842584
value = GetMsdataAttribute(node, Keywords.MSD_DATASETNAME);
2585-
if (value != null && value.Length != 0)
2585+
if (!string.IsNullOrEmpty(value))
25862586
{
25872587
dsName = value;
25882588
}
25892589

25902590
value = GetMsdataAttribute(node, Keywords.MSD_DATASETNAMESPACE);
2591-
if (value != null && value.Length != 0)
2591+
if (!string.IsNullOrEmpty(value))
25922592
{
25932593
dsNamespace = value;
25942594
}
@@ -2597,7 +2597,7 @@ internal void HandleDataSet(XmlSchemaElement node, bool isNewDataSet)
25972597
SetExtProperties(_ds, node.UnhandledAttributes);
25982598

25992599

2600-
if (dsName != null && dsName.Length != 0)
2600+
if (!string.IsNullOrEmpty(dsName))
26012601
_ds.DataSetName = XmlConvert.DecodeName(dsName);
26022602

26032603
// _ds.Namespace = node.QualifiedName.Namespace;

src/libraries/System.Data.Common/src/System/Data/xmlsaver.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ internal void HandleColumnType(DataColumn col, XmlDocument dc, XmlElement root,
12501250
XmlNode type;
12511251
string? name = stNode.Name;
12521252

1253-
if (name != null && name.Length != 0)
1253+
if (!string.IsNullOrEmpty(name))
12541254
{
12551255
// For remoting, always need to work with root schema's namespace
12561256
string nSpace = (_schFormat != SchemaFormat.Remoting) ? stNode.Namespace :
@@ -1314,7 +1314,7 @@ internal void HandleColumnType(DataColumn col, XmlDocument dc, XmlElement root,
13141314
else
13151315
{
13161316
string typeName = XmlDataTypeName(col.DataType); // do not update the hashtable, as it will not write msdata:DataType
1317-
if (typeName == null || typeName.Length == 0)
1317+
if (string.IsNullOrEmpty(typeName))
13181318
{
13191319
if (col.DataType == typeof(Guid) || col.DataType == typeof(Type))
13201320
{
@@ -1812,7 +1812,7 @@ internal XmlElement HandleTable(DataTable table, XmlDocument dc, XmlElement sche
18121812
// the type for this element
18131813
DataColumn col = table.Columns[0];
18141814
string _typeName = XmlDataTypeName(col.DataType);
1815-
if (_typeName == null || _typeName.Length == 0)
1815+
if (string.IsNullOrEmpty(_typeName))
18161816
{
18171817
_typeName = Keywords.XSD_ANYTYPE;
18181818
}
@@ -2445,7 +2445,7 @@ private void GenerateTableErrors(DataTable table)
24452445
DataColumn column = table.Columns[colNum];
24462446
string error = row.GetColumnError(column);
24472447
string columnPrefix = (column.Namespace.Length != 0) ? column.Prefix : string.Empty;
2448-
if (error == null || error.Length == 0)
2448+
if (string.IsNullOrEmpty(error))
24492449
{
24502450
continue;
24512451
}
@@ -2770,7 +2770,7 @@ internal static bool RowHasErrors(DataRow row)
27702770
{
27712771
DataColumn column = row.Table.Columns[colNum];
27722772
string error = row.GetColumnError(column);
2773-
if (error == null || error.Length == 0)
2773+
if (string.IsNullOrEmpty(error))
27742774
{
27752775
continue;
27762776
}
@@ -2794,7 +2794,7 @@ internal void SaveDiffgramData(XmlWriter xw, Hashtable rowsOrder)
27942794

27952795
string prefix = (_ds != null) ? ((_ds.Namespace.Length == 0) ? "" : _ds.Prefix) : ((_dt!.Namespace.Length == 0) ? "" : _dt.Prefix);
27962796

2797-
if (_ds == null || _ds.DataSetName == null || _ds.DataSetName.Length == 0)
2797+
if (_ds == null || string.IsNullOrEmpty(_ds.DataSetName))
27982798
_xmlw.WriteStartElement(prefix, Keywords.DOCUMENTELEMENT, (_dt!.Namespace == null) ? "" : _dt.Namespace);
27992799
else
28002800
_xmlw.WriteStartElement(prefix, XmlConvert.EncodeLocalName(_ds.DataSetName), _ds.Namespace);
@@ -2852,7 +2852,7 @@ internal void Save(XmlWriter xw, bool writeSchema)
28522852
}
28532853
else
28542854
{
2855-
if (_ds.DataSetName == null || _ds.DataSetName.Length == 0)
2855+
if (string.IsNullOrEmpty(_ds.DataSetName))
28562856
_xmlw.WriteStartElement(prefix, Keywords.DOCUMENTELEMENT, _ds.Namespace);
28572857
else
28582858
_xmlw.WriteStartElement(prefix, XmlConvert.EncodeLocalName(_ds.DataSetName), _ds.Namespace);

0 commit comments

Comments
 (0)