This question already has answers here:
C# Field Naming Guidelines?
(17 answers)
Closed 9 years ago.
A question about naming local variables in a C# method that takes a parameter of the same name
Please see the code below
private int DoSomething(string activationCode)
{
...
int ??WhatNameToChooseHere?? = Convert.ToInt32(activationCode);
...
}
What could be a good strategy to apply in the above scenario
Note: method paramter and local variable only differ by type only
You cannot do that. A proper way of doing it is to name your variables with different names.
private int DoSomething(string activationCodeStoredInStringAndPassedAsAnArgument) {
int activationCodeThatDeclaredAsAnIntegerToStoreTheValueConvertedFromString = Convert.ToInt32(activationCodeStoredInStringAndPassedAsAnArgument);
}
Related
This question already has answers here:
What does the [Flags] Enum Attribute mean in C#?
(14 answers)
What does square bracket [] mean in the below code?
(2 answers)
what is [] brackets in .net? [duplicate]
(7 answers)
Closed 6 years ago.
Sorry for the silly question, but I came across the following C# code and I'm wondering what the [Flags] portion is and what it does.
[Flags]
public enum UserFlags
{
//...
}
Thank you in advance.
It's a class attribute. There are also method attributes for example.
You can even write your own
They are usually used to define meta information or behaviour about a class or method and can be read using reflection.
This question already has answers here:
What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?
(12 answers)
Closed 7 years ago.
Does anybody know how to modify a static variable from a different script in Unity?
Please, provide more information about how are you trying to do this...
If you need to change between mono behaviours, for example, you will need to get current instance of behaviour you like to change in a way like
myGameObjectInstance.GetComponent<MyBehaviourWithStatic>().myStatic = value;
but note that user2320445 answer is not wrong, it depends on your context. Be more specific on your question and we could provide better and precise answers
This question already has answers here:
Parameter in C#
(4 answers)
How do the In and Out attributes work in .NET?
(2 answers)
Closed 10 years ago.
Once I know what this means, there's probably a better way of expressing this question, but I don't know where to start.
Here's the Read method for a StreamReader:
public override int Read([In, Out] char[] buffer, int index, int count) { ... }
That "In, Out" bit - what is it for, what does it do, what's it called?
That "In, Out" bit - what is it for, what does it do, what's it called?
They are parameter attributes.
In this case System.Runtime.InteropServices.InAttribute and System.Runtime.InteropServices.OutAttribute which are used for interop with code outside the .NET runtime.
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.
This question already has answers here:
Implicit typing; why just local variables?
(6 answers)
Closed 8 years ago.
Why is it not possible to have implicitly-typed variables at a class level within C# for when these variables are immediately assigned?
ie:
public class TheClass
{
private var aList = new List<string>();
}
Is it just something that hasn't been implemented or is there a conceptual/technical reason for why it hasn't been done?
Here's a blog post from Eric that explains the reasoning.