Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryCast returns Warning BC4322 on String #23923

Open
paul1956 opened this issue Dec 24, 2017 · 2 comments
Open

TryCast returns Warning BC4322 on String #23923

paul1956 opened this issue Dec 24, 2017 · 2 comments

Comments

@paul1956
Copy link
Contributor

Version Used:
Vs 2017 15.5.2
Steps to Reproduce:

  1. The description of TryCast says that it will not throw an exception, and this error only appears on the line where I am trying to assign the result to a string. String does implement IEnumerable(Of Char).
    Below is not a complete implementation of this routine just enough to show the warning and several cases without the warning.
       Function IsEmpty(Of T)(ByVal lSource As IEnumerable(Of T)) As Boolean
            Dim readOnlyCollection As IReadOnlyCollection(Of T) = TryCast(lSource, IReadOnlyCollection(Of T))
            If readOnlyCollection IsNot Nothing Then
                Return readOnlyCollection.Count = 0
            End If

            Dim genericCollection As ICollection(Of T) = TryCast(lSource, ICollection(Of T))
            If genericCollection IsNot Nothing Then
                Return genericCollection.Count = 0
            End If

            Dim collection As ICollection = TryCast(lSource, ICollection)
            If collection IsNot Nothing Then
                Return collection.Count = 0
            End If

           ' The following line gets Warning on iSource
            Dim str As String = TryCast(**lSource**, String)
            If str IsNot Nothing Then
                Return str.Length = 0
            End If
            Return True
        End Function
  1. With the code above there should be no warnings
    Expected Behavior:
    No Warnings
    Actual Behavior:
    Warning BC42322 Runtime errors might occur when converting 'IEnumerable(Of T)' to 'String'.
@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented Dec 26, 2017

IEnumerable(Of Char) is trying to be cast as a String. Not String being cast to IEnumerable(Of Char)

@paul1956
Copy link
Contributor Author

@AdamSpeight2008 good point, it works in C#.

		public static bool IsEmpty<T>(this IEnumerable<T> source)
		{
			var readOnlyCollection = source as IReadOnlyCollection<T>;
			if (readOnlyCollection != null) {
				return readOnlyCollection.Count == 0;
			}

			var genericCollection = source as ICollection<T>;
			if (genericCollection != null) {
				return genericCollection.Count == 0;
			}

			var collection = source as ICollection;
			if (collection != null) {
				return collection.Count == 0;
			}

			var str = source as string;
			if (str != null) {
				return str.Length == 0;
			}
			return true;
		}

@jaredpar jaredpar added the Bug label Aug 31, 2018
@jaredpar jaredpar added this to the Unknown milestone Aug 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants