Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am looking for reading .Net, C# reserved key words programmatically in VS 2015.
I got the answer to read C# reserved words in the [link][1].
CSharpCodeProvider cs = new CSharpCodeProvider();
var test = cs.IsValidIdentifier("new"); // returns false
var test2 = cs.IsValidIdentifier("new1"); // returns true
But for var, dynamic, List, Dictionary etc the above code is returning wrong result.
Is there any way to identify .net keywords in run time instead of listing key words in a list?
string[] _keywords = new[] { "List", "Dictionary" };
This is a perfectly fine C# program:
using System;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int var = 7;
string dynamic = "test";
double List = 1.23;
Console.WriteLine(var);
Console.WriteLine(dynamic);
Console.WriteLine(List);
}
}
}
So your premise is wrong. You can find the keywords by looking them up in the short list. Just because something has a meaning does not mean it's in any way "reserved".
Do not let the online syntax highlighting confuse you. Copy and paste it into Visual Studio if you want to see proper highlighting.
As explained by nvoigt, your method of programmatically determining if a string is a keyword is effectively correct. To be complete, (after checking Reflector) it should be:
bool IsKeyword(string s)
{
var cscp = new CSharpCodeProvider();
return s != null
&& CodeGenerator.IsValidLanguageIndependentIdentifier(s)
&& s.Length <= 512
&& !cscp.IsValidIdentifier(s);
}
(The VB.NET version needs 1023 and a check for "_".)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
In the Main function, declare three integer variables (name them arbitrarily) and initialize these variables with values (ideally different). Write a program that computes the following arithmetic expression: Multiply the values of the last two variables and subtract the value of the first variable from the obtained result. Write the arithmetic expression and its result on the screen in a suitable way.
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int prvni = 10;
int druha = 20;
int treti = 30;
int vysledek = (treti * druha) - prvni;
Console.WriteLine("Výsledek: {vysledek}");
}
}
}
String-interpolation in that way requires a $ (dollar-sign) before the string to specify that you are doing interpolation, so: Console.WriteLine($"Výsledek: {vysledek}");
For more examples on string interpolation: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
An alternative solution could be to simply concatenate the variable to the string: Console.WriteLine("Výsledek: " + vysledek);
You need to print the varable correctly.
Console.WriteLine("the answer {0}", vysledek);
Take care,
Ori
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Newb to C# and OOP. My journey thus far has been to take code bases that I've inherited from former developers and either address issues, or add enhancements, whilst trying to understand said code bases' structures from front-to-back.
I'm having trouble fully grasping the concept around the parameters which follow the initial declaration of a method. Here's an example of a method I'm working with:
public List<Entity> ParseCsvFile(List<string> entries, string urlFile)
{
entries.RemoveAt(entries.Count - 1);
entries.RemoveAt(0);
List<Entity> entities = new List<Entity>();
foreach (string line in entries)
{
Entity entityManagement = new Entity();
string[] lineParts = line.Split('|');
entityManagement.Identifier = lineParts[0];
entityManagement.ProductId = 1234;
entityManagement.Category = "ABCDE";
entities.Add(entityManagement);
}
return entities;
}
The part after ParseCsvFile in parentheses: (List<string> entries, string urlFile)
Could someone explain what these are and what they do, perhaps with metaphors/analogies/real-world examples?
It might be easier to see their purpose if you look at a simpler function for example:
public int Add(int number1, int number2)
{
return number1 + number 2;
}
Above there is a function that adds two numbers together and returns the result. It is a set of instructions to follow. How can it follow the instructions if it doesn't know what numbers to use.
That's where calling the function comes in.
for example:
var result = Add(2, 5);
In this scenario result = 7.
2 is replacing number1 in the function and 5 is replacing number2.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Ok, as my original question seemed a bit ambiguous because I was asking for a general question about the C# language, but showing part of a particular example where I was having a problem with it, I'm going to try to rewrite so that it is clearer that my question is about the C# language, not about my particular problem.
I currently have a property (several, in fact) of a class, that return a different value depending on whether you access them directly by code, or using reflection. This is what happens when I access the property using the immediate console of VS:
> SelectedLine.QtyOutstanding
0
> var prop = SelectedLine.GetType().GetProperty("QtyOutstanding")
> prop.GetValue(SelectedLine)
8
Regardless of how the property is defined, what is the difference, in C#, between both ways of accessing the property?
Shouldn't they both run exactly the same code in the setter/getter, if there is one?
(Considering that GetType() returns the same type as the variable is declared as)
I found a way to produce this, maybe your case looks like that?
If your SelectedLine is accessible via interface, and your class has an explicite implementation of that, but also has a public property with the same name, this could lead to different results.
Example
class Program
{
static void Main(string[] args)
{
var SelectedLine = (ILine)new Line(8);
Console.WriteLine(SelectedLine.QtyOutstanding); // 0
var prop = SelectedLine.GetType().GetProperty("QtyOutstanding");
Console.WriteLine(prop.GetValue(SelectedLine)); // 8
Console.ReadLine();
}
}
class Line : ILine
{
public Line(int qtyOutstanding)
{
QtyOutstanding = qtyOutstanding;
}
public int QtyOutstanding { get; }
int ILine.QtyOutstanding
{
get
{
return 0;
}
}
}
interface ILine
{
int QtyOutstanding { get; }
}
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 6 years ago.
Improve this question
I am currently getting into Pentesting and Ethical Hacking to test website security.
I would appreciate an example Bruteforce algorithm that is stored in a string. Not a dictionary algorithm, but a bruteforce algorithm. For example, it tries the letter a. Then it tries the letter b, then it tries c and so on. Thank you in advance :)
Even if i think that you should really come up with an own concept for this problem, i'll like to give you a hint how to do this.
Disclaimer: this is the laziest, slowest and dirtiest approach possible but it gets its job done. NEVER EVER USE THIS FOR A REAL SYSTEM.
Programm.cs
class Program
{
static void Main(string[] args)
{
Brutforce b = new Brutforce()
{
Alphabet = new []{'a', 'b', 'c', 'd'}
};
ICollection<string> vals = b.Calculate(3);
foreach (var elem in vals)
Console.WriteLine(elem);
Console.ReadKey();
}
}
Brutforce.cs
internal class Brutforce
{
public ICollection<char> Alphabet { get; set; }
private ICollection<string> _calculate(int lenght)
{
if (lenght <= 1) return Alphabet.Select(a => a + "").ToList();
ICollection<string> sub = _calculate(lenght - 1);
return (from alpha in Alphabet from prior in sub select alpha + prior).ToList();
}
public ICollection<string> Calculate(int lenght)
{
return Alphabet == null ? null : _calculate(lenght);
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In python to print a list of 11 Variables i would do so using "Exec". There is a list of 11 Items, this code prints them all.
for count in range(1,11):
question = ("print " + "question" + str(count))
exec question
How would I do something similar in C# ?
(Without the use of lists)
Here's what i have so far
string line;
for (int i = 1; i < 200; i++)
{
line = ("Console.WriteLine(scene1_f"+i);
// Execute "line"
}
Thanks.
I think it would be wise if you read a few things on the c# language while you are trying out stuff. At the same time, it would be wise if I looked up some Python tutorials before attempting to answer such questions.
Although c# supports dynamic types and expressions, it is mostly used to create "strongly typed" constructs. For the example you provided I believe there are no easy / simple direct translations.
Your "scene1_f1" through "scene1_f200" variables would likely be instances of some c# type like a Scene class, that has some properties and methods that operate on the object instance.
If you have multiple Scene object that you want to perform the same type of operation on (like printing them to the console as in your example), it is generally considered good practice to group them in some manner, such as adding them to a List or storing them in an Array.
To illustrate what I mean I have added a hypothetical example:
public class Scene
{
public Scene(string name)
{
Name = name;
}
public string Name { get; set; }
// ... more properties
public void Draw()
{
// logic for drawing
}
// ... more methods.
public override string ToString()
{
// here return what you would want to have as
// a string representation of a Scene object.
return "Scene " + Name;
}
}
// in a different part of your code, create and add the Scene objects
var scenesList = new List<Scene>();
scenesList.Add(new Scene("Some scene name"));
// add more
// Now you can print them to the console like this:
foreach (var scene in scenesList)
Console.WriteLine(scene);