Tagged Questions
5
votes
3answers
94 views
Why c# compiler generates a compile error? [duplicate]
I have following code
using(some code)
{
var b = ....
}
var b = ...
Erorr: A local variable named 'b' cannot be declared in this scope because it would give a different meaning to 'b', which is ...
5
votes
1answer
52 views
Internal Implementation of AsEnumerable() in LINQ
I have two questions:
Question 1 Background :
I noticed when looking at the implementation of 'AsEnumerable()' method in LINQ from Microsoft, which was:
public static IEnumerable<TSource> ...
1
vote
3answers
43 views
Why nullable int (int?) doesn't increase the value via “+=” if the value is NULL?
I have a page counter type of int?:
spot.ViewCount += 1;
It works ONLY if the value of ViewCount property is NOT NULL (any int).
Why the compiler do so?
I would be grateful for any solutions.
-4
votes
2answers
45 views
Use of local unassigned variable - even with else-statement
MediaDescription media;
foreach(var field in fields) {
switch(field.Key) {
case FieldType.Version:
message.Version = Convert.ToInt32(field.Value);
break;
...
0
votes
2answers
24 views
AOP Pre-compile time weaving?
I have been playing along with AOP for years, but didnt become 100% satisfied with the solutions.
AOP Frameworks with runtime weaving, like Spring.AOP, cannot change the interface of a class.
With ...
2
votes
3answers
60 views
How is non-virtual instance method inheritance resolved?
Quoted from CLR via C#, it reads to me as if call would search for the method that is defined by a base type at runtime via CLR.
Then call IL instruction is used to call an instance or virtual
...
0
votes
0answers
26 views
preprocessor or conditional compilation in c# based on resource availability
I'm working on the same project on two different computers, the project uses either of two different library that preform the same function in the project. i.e. I have a module for each library, both ...
4
votes
2answers
87 views
Does the .exe file produced by the C# compiler consist solely of Common Intermediate Language(CIL)?
I am just trying to get a better grasp on what happens in a simple scenario when the following code is added to a file and saved with the .cs extension:
public class HelloWorld
{
public static void ...
0
votes
0answers
22 views
Extract commands, replace values and process output View
I have built a simple CMS. it replace field values in model in a loop for every requested records.
like:
<cmsEntity name="page">
<where statement="ParentRecordId=@1">
...
0
votes
2answers
29 views
How to escape whitespace using csc.exe (.NET built in C# compiler)
Like the title says, how do I escape white space passed in a file name to csc.exe? Caret's cannot be used. For example, file path C:\Users\user name\My Documents\file.cs I would normally do ...
1
vote
3answers
55 views
Is it possible to check compiler constants at runtime in C#?
I know I can conditionally set variables by conditionally including code like so:
#if DEBUG
someVar = "foo";
#else
someVar = "bar";
#endif
I would rather enumerate or test for compiler ...
0
votes
0answers
36 views
Passing a variable declared in action code to parser code in C# cup?
I'm making a .cup for C# and I'm building a symbol table with it. My symbol table nothing more than a List. I declared this List inside my action code in order to be able to use it, but I don't know ...
1
vote
3answers
59 views
C# - Invoke Event Outside Of Declaring Class
I have an event in class Alice that I want to raise inside of a derived class Bob:
public class Alice
{
public event Action<object> ValueChanged;
}
public class Bob : Alice
{
public ...
9
votes
5answers
394 views
Field initializer accessing `this`: invalid in C#, valid in Java? [closed]
First, an introduction:
This code:
class C
{
int i = 5;
byte[] s = new byte[i];
}
fails to compile with the following error:
A field initializer cannot reference the nonstatic field, ...
4
votes
3answers
124 views
Is the C# or JIT compiler smart enough to handle this?
Assuming we have the following model:
public class Father
{
public Child Child { get; set; }
public string Name { get; set; }
public Father() { }
}
public class Child
{
public ...
4
votes
2answers
125 views
Why am I getting this definition reference error?
I am using TaskBar methods defined within the namespace Microsoft.WindowsAPICodePack.Taskbar. Specifically, I'll focus on SetProgressState for this question.
Here is the meta-definition I get when I ...
1
vote
0answers
43 views
Why is throw null not a compiler warning? [duplicate]
Why is throw null not a compiler warning in C#?
I saw some code which had a throw null; in it. This causes a new NullReferenceException to be thrown.
Why would the compiler not flag this with a ...
0
votes
2answers
109 views
trying to optimize if/else condition slows down program
I am currently trying to optimize a .net application with the help of the VS-Profiling tools.
One function, which gets called quite often, contains the following code:
if ...
6
votes
1answer
74 views
Does C# compiler convert and store static variables?
My program uses the code:
Convert.ToDouble(Int32.MaxValue)
Fairly regularly. I was just curious about how this is handled by the compiler. Is it stored as a static double or is it executed in run ...
-1
votes
1answer
74 views
Different between x++ and ++x [duplicate]
I've known that primary operator (x++) is different form unary operator (++x) when combine with another operator in a statement.
But I wonder whether those two operator is same when leave them alone ...
5
votes
2answers
111 views
compiler optimisation when returning strings
If I have
private string Foo(string decrypted)
{
return decrypted.Substring(blah);
}
and
private string Foo(string decrypted)
{
string s = decrypted.Substring(blah);
return s;
}
Is ...
4
votes
4answers
106 views
Dynamically use runtime compiled assemlby
I compile code dynamically from code:
string code = @"
namespace CodeInjection
{
public static class DynConcatenateString
...
0
votes
1answer
63 views
CompileAssemblyFromDom throws access denied exception
The code:
using (var codeProvider = new CSharpCodeProvider())
{
var compilerParameter = new CompilerParameters(
_assemblies, assemblyName, false)
{
...
0
votes
0answers
49 views
VS tries to compile Resharpers decompiled code
Sometimes when I have looked at decompiled code using Resharper's decompiler and then try to build my project, VS tries to compile the decompiled code that Resharper generated for me. When I look at ...
1
vote
1answer
33 views
Debugger shows invalid instruction
Sometimes it happens, that Visual Studio debugger shows invalid current instruction. The simplest possible reasons are:
Outdated .pdb files. These files contain information about which offset of the ...
1
vote
1answer
51 views
Generating big files in .NET
I would like to ask on two things about .NET executables:
.NET executable is in PE format. Does it mean that address generated by CIL compiler is from the beginning of the file ...
0
votes
1answer
27 views
Are there any classes which can supply you with what a MethodInfo/method name would be?
I was wondering if there are any classes which can generate a MethodBase/MethodInfo, or simply generate the method name, without using "magic strings". Right now I'm doing the following:
public void ...
0
votes
2answers
35 views
Getting an object property multiple times in a row vs getting it once and storing in a variable. Will the compiler optimize it anyway?
Is it better to store an object property when you access it multiple times in a row? Below is a rather silly example of getting an object property multiple times in a row and two ways of dealing with ...
0
votes
3answers
76 views
how tell the IDE not compile certain files?
I've added a bunch of files from my older project into my new project. They're still not playing an active role in my current code and there are missing classes that won't make them fully functional ...
0
votes
1answer
48 views
Initialize object by assignment?
How to give types the ability to initialize via an assignment, some like the following:
public struct WrappedByte
{
private byte m_value;
}
//Usage:
WrappedByte x = 0xFF;
0
votes
1answer
57 views
Compiler warning 'await' operator, should I ignore it?
I have a little button code like this:
private void CreateButton_Click(object sender, RoutedEventArgs e)
{
var MsgDialog = new MessageDialog("MY MESSAGE");
MsgDialog.Commands.Add(new ...
0
votes
1answer
368 views
A lambda expression with a statement body cannot be converted to an expression tree
There are several questions on StackOverlow about statement body expressions. I am just wondering why it is not possible to do this in C#? (Where is the complexity why it was not implemented in the ...
3
votes
2answers
979 views
Javascript and C# Cross Compiling and Conversion
What are the various tools to cross-compile or convert Javascript to C# and back? And how to execute JS in C# or C# in JS? This is a popular question I will provide an answers for.
2
votes
1answer
115 views
Reflection.Emit.ILGenerator::Emit, call method from third party .net library
I'm trying to create simple .net compiler for educational purposes. After parsing, scanning and building AST I'm generating .net assembly using Reflection.Emit.ILGenerator.
Here is my sample code for ...
1
vote
2answers
76 views
IL code for attribute inheritance
I have a basic question here.
I have an attribute on an abstract method in a base class. Now when I implement/override this method in some derived class I do not see the attribute being applied in ...
14
votes
3answers
693 views
Why null == false does not result in compile error in c#? [duplicate]
This is not to solve any particular problem. Simply a compiler question.
Why does the following code not result in compile error? It's comparing a reference type to primitive type. Both null and ...
2
votes
1answer
98 views
Will the C# compiler remove unused local if it is assigned a property?
This might be a silly question. I know that compiler will remove unused locals. But if I write my code like this:
class MyClass
{
public int SomeProperty
{
get
{
...
...
2
votes
4answers
177 views
'|' vs '||' compiler optimization in C#
I was recently asked this question in an interview which I totally got wrong but got me curious about the compiler optimizations in C# and .net
Consider the following snippet:
void Main()
{
...
89
votes
3answers
2k views
Why does this (null || !TryParse) conditional result in “use of unassigned local variable”?
The following code results in use of unassigned local variable "numberOfGroups":
int numberOfGroups;
if(options.NumberOfGroups == null || !int.TryParse(options.NumberOfGroups, out numberOfGroups))
{
...
0
votes
0answers
33 views
Read the error report of the csc.exe
we are running compilation from script of some sources , by calling csc.exe
Is there any way to get error information in some structured xml file ? I mean , without parsing stdouptut. Maybe there is ...
3
votes
2answers
96 views
C# Performance penalty for Int32 literals to floats
In c#, you can define a number literal as either int or double
Double var1 = 56.1;
int var2 = 51;
These are the default types the literals are assigned. However, the game engine I'm working ...
0
votes
4answers
140 views
C# Compile Visual Studio Program [closed]
I'm trying to create a program which compiles another program a bunch of times, each time adjusting some constant variables in one of the class source files (like hard coded configuration).
I need ...
0
votes
0answers
54 views
Performance Analysis with PerfView
I just have created a simple console application emitting a bunch of code in memory. I’m trying to gather useful data using PerfView.
When I want to drill down after the last static compile-time ...
31
votes
3answers
809 views
If Int32 is just an alias for int, how can the Int32 class use an int?
Been browsing through .NET source code of .NET Framework Reference Source, just for fun of it. And found something I don't understand.
There is a Int32.cs file with C# code for Int32 type. And ...
1
vote
1answer
275 views
C# 4.0 Antlr4 run-time [closed]
Does any one knows, when is other non-java run-time especially C# 4.0 would be available.
Earlier Antlr 3.0 versions got run-times almost for all popular programming languages.
0
votes
4answers
58 views
How to force 2 C# methods always appear together in compile time?
I have two C# methods RegisterTasks() and WaitForAll(), and whenever RegisterTasks() is called, WaitForAll() must be called in the same code block too. Just wondering if there's a way to tell C# ...
1
vote
3answers
76 views
Interface method marked as Obsolete does not issue a message from the compiler when implemented
Consider this example
public interface IAnimal
{
[Obsolete("Animals can't eat anymore", true)]
void Eat();
}
public class Animal : IAnimal
{
public void ...
2
votes
1answer
299 views
Mixing .NET 3.5 with 4/4.5 assemblies in the same solution/project
I have a solution that most projects are compiled in .net 4.5 (I use async and tasks).
There are couple of projects that need to be compiled in .net 3.5 since they use SDK that support only .net 3.5.
...
0
votes
1answer
61 views
Compile multiple times the same .dll in runtime
I am working on a simple 2D game editor, I want to be able to read from a .cs file, compile and then attach it to objects in runtime (like Unity3D) and be able to edit its properties.
I am compiling ...
1
vote
1answer
77 views
CompilationErrorException when including System.Linq in Roslyn CP2
I have downloaded the Roslyn CTP and have run across the following error.A CompilationErrorException is thrown when executing the line session.Execute(@"using System.Linq;"); with the following ...