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

Variable never initialized with value #45381

Closed
LaviniaSurubariu opened this issue Mar 18, 2025 · 0 comments · Fixed by #45431
Closed

Variable never initialized with value #45381

LaviniaSurubariu opened this issue Mar 18, 2025 · 0 comments · Fixed by #45431
Assignees
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. ⌚ Not Triaged Not triaged

Comments

@LaviniaSurubariu
Copy link

LaviniaSurubariu commented Mar 18, 2025

Type of issue

Code doesn't work

Description

  • In the DisplayGenericParameter method, variable classConstraint is initialized with null and then is checked without getting any value.

      Type classConstraint = null;
    
      foreach(Type iConstraint in tp.GetGenericParameterConstraints())
      {
          if (iConstraint.IsInterface)
          {
              Console.WriteLine("         Interface constraint: {0}",
                  iConstraint);
          }
      }
    
      if (classConstraint != null)
      {
          Console.WriteLine("         Base type constraint: {0}",
              tp.BaseType);
      }
      else
      {
          Console.WriteLine("         Base type constraint: None");
      }
    
  • After changing it to

       Type classConstraint = tp.BaseType;
    

it is displayed the right base type constraint when executing this line of code :

     DisplayGenericType(typeof(Test<>));

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/reflection/how-to-examine-and-instantiate-generic-types-with-reflection

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/reflection/how-to-examine-and-instantiate-generic-types-with-reflection.md

Document Version Independent Id

100f6dac-701d-701c-0e4b-4308570887a6

Platform Id

b0ffbd50-46af-d46a-1a52-59f6d5790be1

Article author

@dotnet-bot

Metadata

  • ID: 06896d3a-bd8d-60bd-e652-211bd7454d80
  • PlatformId: b0ffbd50-46af-d46a-1a52-59f6d5790be1
  • Service: dotnet-fundamentals

Related Issues

@dotnet-policy-service dotnet-policy-service bot added the ⌚ Not Triaged Not triaged label Mar 18, 2025
@gewarren gewarren mentioned this issue Mar 19, 2025
@gewarren gewarren self-assigned this Mar 19, 2025
@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. ⌚ Not Triaged Not triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants