Rename Variable in c# [duplicate] - c#

This question already has answers here:
Create dynamic variable name
(5 answers)
Closed 4 years ago.
I want to change the variable name to the string I entered.
For example :
public int MyVariable;
public string Name="MyName";
I want Rename 'MyVariable' to 'MyName'.How ?

You can't because C# is a strongly-typed language.
See Types (C# Programming Guide).
C# is a strongly-typed language. Every variable and constant has a
type, as does every expression that evaluates to a value. Every method
signature specifies a type for each input parameter and for the return
value. The .NET class library defines a set of built-in numeric types
as well as more complex types that represent a wide variety of logical
constructs, such as the file system, network connections, collections
and arrays of objects, and dates. A typical C# program uses types from
the class library as well as user-defined types that model the
concepts that are specific to the program's problem domain.

Related

Getting a type representation of a partially open generic type [duplicate]

This question already has answers here:
C# Language: How to get type of bound but open Generic class?
(4 answers)
Closed 6 years ago.
I am wondering if anyone knows a way to get a representation of a partially open generic type in C#, for example IDictionary<string,>.
What I have tried:
typeof(IDictionary<string,>)
I get: Partially opened type is not permitted in 'typeof' expression compile error.
typeof(IDictionary<,>).MakeGenericType(typeof(string))
I get: ArgumentException (The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation)
Why I want to do this:
I have a number of scenarios where I need to check if a class I have implements an interface, but in several cases I know that some of the generic type parameters have to be specific (i.e. I want something that implements a dictionary with string keys but I don't care about the value type). I realize there are a number of other ways I could do this (for example, by providing an array of necessary generic parameter types to my method). But in looking at the problem I got curious if there is a way to specify partially open / partially closed generic types, hence the question.
Maybe something like that could answer to your problem:
class CustomDictionary<TValue> : Dictionary<string, TValue>
{
}
And this should work:
typeof(CustomDictionary<>).MakeGenericType(typeof(string))

What are the differences between C++ and C# primitive data types? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to figure out the differences between C++ and C# data types. I know C# and java differ because data types are stored as objects in C# instead of having the core class library providing a wrapper class to represent the data types as a Java object. However I can't find much on the differences of C# and C++ data types...
The difference you describe is wrong. Java, C# and C++ all treat the primitives as basic objects. C and C++, being low-level languages, keep them that way - they are unique to the compiler as primitives.
In Java, there exist thin wrappers, such as java.lang.Integer which is a class containing a single int member variable.
C# can implicitly treat a primitive as an object, and will on the fly convert for example an int to a System.Int32 as required by various situations. The process is called Boxing and Unboxing, of which the first is implicit and the second is explicit. For further reference see the linked article.
To put it simply, C#'s primitive types like int bool, short etc... are organized as structures, in contrary with C++ primitive types which are not structures.
For example, in the C# on the int primitive type itself you can call some methods (for example you can call methods Parse or Equals). This is also true for the bool primitive type.
To go even further, Int32 and int are totally the same types in the C#, as well as bool and Boolean are. So the int, bool, short etc... are keywords in the C# which are actually masking the following structures Int32, Boolean, Int16. You can try it by calling:
int a=int.MaxValue;
Int32 b = a;
In the first line we are creating variable a which type is int. The value of the variable a is set to the int.MaxValue which is actually constant defined in the type int or to be more precisely Int32.
On the second line value of the variable b becomes the value of the variable a. This
confirms that a and b are the variables of the same type, otherwise, an error would occur.
At the other hand, in the C++, primitive types are not organized as structures, so you can't call any method on the primitive type or the primitive type instance. These are also called compiler primitives.

Why anonymous type are immutable in c#? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are the properties of anonymous types in C# read-only?
I wrote something like this,
var suspense = new { Name = "Android", Market = string.Empty };
suspense.Market = "Potential";
.NET throws error
Property or indexer 'AnonymousType#1.Market' cannot be assigned to --
it is read only
I know that AnonymousTypes in C# are immutable, but why? Is this due to some limitation with CLR?
The motivating factor for driving the immutable anonymous types was because the LINQ APIs used hash tables internally and returning projections of anonymous types that could be modified was a dangerous situation.
You can check :
Immutable types: understand their benefits and use them
Anonymous Types and Object Identities By Tim Ng on MSDN

string is a Class-why [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, why is String a reference type that behaves like a value type?
Why in C# string is a class/ref type , where as int/double are value/struct- any specific reason or it is by design
Integral types have the important property of being accessible as a whole by the processor in one go. It is not the case for a string which may be composed of thousands of bytes, so in all languages, strings have always been pointed to, because the computer cannot really do it any other way.
In an object language like C#, it is canonical to create a class to point to a memory location: that's actually what an object is about.
So yes, strings are classes, because they can't be integral types.

Type Name aliasing in C# [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
typedef in C#?
STL like containter typedef shortcut?
I was wondering if there was an equivalent to Type aliasing from Functional Languages that I can use in C#
For example in a nice functional language like Haskell I can say something like the following to alias an existing type to a custom Type Name
type MyCustomTypeName = String
I'd like to do this as I have an API that I'm building where some of the Objects I'm using have multiple possible names in the sense they could be referred to by several terms which are interchangeable and equivalent. Presumably I could do this with inheritance but that seems somewhat clunky and then potentially breaks if people start extending the non-canonical class ie.
public class CanonicalClass {
//Full Implementation
}
public class AlternateName : CanonicalClass {
//Empty except I'll need to redefine all the constructors
//Could declare it sealed but doesn't get rid of the need to redefine constructors
}
And before anyone mentions interfaces all the Classes in question are all implementing interfaces already and there are multiple differing implementations of these interfaces.
Depending on what you're actually trying to do (give a somewhat more complete example), you may indeed need interfaces (used properly) and/or generics.

Categories