opposite of nameof (string of variable name to variable) C# [duplicate] - c#

This question already has answers here:
Accessing an instance variable by name (string), kinda like dynamic languages do, in C#
(3 answers)
How to get a property value based on the name
(8 answers)
Closed last year.
Say I have a List called this_list. If I make a string that says "this_list", how can I turn that string into the actual variable? It should do the opposite of what nameof would do.
List<string> this_list = new List<string> { "wow","amazing" };
string str = "this_list";
// str to this_list somehow

If this_list is a local variable (like in your example), you can't. Local variable names are lost during compilation.
If this_list is a field or property, you can use Reflection.

Related

C# Add conditional directly inside string [duplicate]

This question already has answers here:
C# interpolated string with conditional-operator [duplicate]
(2 answers)
Closed 4 months ago.
I have a boolean isEuropean and based on it's value I want to write
Console.WriteLine("This individual is/is not European");
Is it possible in C# to add a conditional directly inside a string with no additional variables created?
bool isEuropean = true;
Console.WriteLine($"This individual {(isEuropean ? "is" : "is not")} European");
Yes, you can do string interpolation with a ternary, make sure you contain the ternary in parentheses.

How to use the `record` as a type name and resolve the ambiguity with the `record` keyword in C#? [duplicate]

This question already has answers here:
How do I use a C# keyword as a property name?
(1 answer)
C# # operator (not for string literals)
(4 answers)
What's the use/meaning of the # character in variable names in C#?
(9 answers)
# prefix for identifiers in C#
(5 answers)
Closed 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
How to resolve the ambiguity between the record type name and the record keyword in C#?
I've seen this SO question it is about 13 years ago but this option is a new feature and related more to modifiers. On that post you can't use class alone in context. But here you can define and use record every where without any prefix. you just resolve the ambiguity with single-time prefix.You can not use class as a property name nowhere but for record it is possible. Just somewhere creates ambiguity.
What's the use/meaning of the # character in variable names in C#?
C# # operator (not for string literals)
# prefix for identifiers in C#
These SO posts are all about adding # before a keyword. You can not use that keyword alone(without #) as a variable. But this new C# feature is for resolving ambiguity. You have not to use # every where, but just at ambiguous situations.
Example for the difference:
var #int = 3;
int= 5; //error❌
var #record = 5;
record = 5;//OK✅
Question example:
class record
{
public record()
{
}
record Name() //ambiguity record type name with record keyword
{
return new record(); //Compile time error
}
}
Just put an at-sign # before the record type.
By placing an # before the type name you explicitly tell the compiler that it is a type name not a keyword.
class record
{
public record()
{
}
#record Name() //✅
{
return new record();
}
}
You can use # in every similar situation.
For more info refer to Warning on lower case type names in C# 11

Cannot implicitly convert type string to Autodesk.Revit.BD.DisplayUnitType [duplicate]

This question already has answers here:
Convert a string to an enum in C#
(29 answers)
Closed 2 years ago.
I am using sqlDatareader to read and assign variable value. The variable datatype is string.
string displayUnits = dr["DisplayUnits"].ToString(); // the variable value "DisplayUnitType.DUT_GENERAL"
Variable value is used to assign formatOptions.DisplayUnits.
formatOptions.DisplayUnits = displayUnits;
However, I am getting an error message cannot implicitly convert type string to Autodesk.Revit.BD.DisplayUnitType. I know I assigned a value to another type.
How do I casting and type conversions?
Indeed, you cannot implicitly convert a string to an enum.
You can explicitly convert a string to an enum in C#, as Chetan already pointed out.

Variable assigned but never used [duplicate]

This question already has answers here:
Variable is assigned but its value is never used (C#)
(4 answers)
Closed 5 years ago.
int price;
if (listBox1.Text == "Regular McYum")
{
price = 70;
}
How come the 'price' variable is assigned but never used?
The warning is just clear: assigning a variable and using it are different things. The first is you set a value to it, the second ones means you´re doing something with that value.
So in your case you should pass your variable to a method for example:
Console.WriteLine(price);
This should be a warning from your compiler, so you can - although you shouldn´t - ignore this.
If listBox1.Text does not equal "Regular McYum", then price will never get set. You need to ensure that price will receive a value on all of the possible code paths.

not allowing to update value for a specific index [duplicate]

This question already has answers here:
c# string character replace
(4 answers)
Closed 7 years ago.
I'm having problem in assigning a value to the specific index in the string after checking on the same index:
Here below is my code:
"bits" is a string and "dirtybit" is an integer.
if (bits.ElementAt(dirtybit).Equals('1'))
bits[dirtybit] = '0'; //shows red underlined error
Error:
Property or Indexer String.this[int] cannot be assigned to -- is only read
Why can I not access the same index (value)?
Is there any workaround?
Strings are immutable in C#. You can not change them after you have created them.
You can use StringBuilder to create a new string.
From MSDN:
Strings are immutable--the contents of a string object cannot be
changed after the object is created, although the syntax makes it
appear as if you can do this.

Categories

Resources