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

Object Type Mismatch When Using var for Initialization #76700

Open
RamsisDev opened this issue Jan 9, 2025 · 1 comment
Open

Object Type Mismatch When Using var for Initialization #76700

RamsisDev opened this issue Jan 9, 2025 · 1 comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@RamsisDev
Copy link

RamsisDev commented Jan 9, 2025

Version Used:
4.12.0-324572.7

Description:
We encountered an issue where initializing an object using var leads to unexpected behavior when the object is passed to a function elsewhere in the repository. Specifically, the function does not recognize the object as the expected type, which results in incorrect or failed functionality.

Steps to Reproduce:

  1. Create an object using var, for example:
  var paragraph = new Paragraph();
  1. Use this object in a function to perform an Add operation, such as:
public Paragraph CrearParagrahp(List<Run> Runs)
{
   var paragraph = new Paragraph();

   foreach (var run in Runs)
   {
       paragraph.Inlines.Add(run);
   }

   return paragraph;
}
  1. Observe the behavior when the function is called and the returned result appears empty or incorrectly initialized if you call it form another instance of a proyect.

Expected Behavior:
The object a should work as expected when used in the function, with the returned data properly initialized and populated.

Actual Behavior:
When var is used to initialize the object, the data being added to it does not appear, and the returned result is empty or incorrectly initialized. However, when the object is explicitly declared with its type, such as:

The function behaves as expected, and the data is correctly initialized and returned.

Diagnostic Id:

OutputShows no Id of error.

Additional Information:

The libraries in use during this issue are:

  • System.Windows.Documents

Note

This issue occurred in a private repository and, therefore, a direct link to the relevant code cannot be provided. If additional context or specific details are needed, please let me know, and I will provide as much information as possible.

Further Analysis:
The issue seems to occur when using var to initialize the object. When this object is later used in a function to perform an Add operation, the data being added to it does not work as expected and does not appear. However, when the object is explicitly declared with its type, it works as intended.

The problem seems to be that, without explicit casting, the data disappears when the variable is returned or passed in the function call.

Could you provide insights into why this behavior occurs with var initialization? Is this expected behavior or a potential bug? Any guidance or clarification would be greatly appreciated.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 9, 2025
@CyrusNajmabadi
Copy link
Member

This needs a repro. I cannot repro the problem at all.

Alternatively, you can just compile your code with 'var' or an explicit type. Then decompile the method with ILSpy to see what the difference is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants