How do keywords work in C#? [closed] - c#

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'm not even sure how should I frame this question so that you all get what actually I'm asking for.
I'm wondering how does the keywords work in programming languages, being specific, C#. In the below code:
using System;
namespace TestApplication
{
class Program
{
static void Main(string[] args)
{
string s = "Hello";
Console.WriteLine(a.ToString());
Console.ReadLine();
}
}
}
Here, Console is a predefined class of System namespace which lies in mscorlib.dll. So when the compiler/CLR meets the Console.WriteLine(), it will invoke the static method WriteLine() with appropriate overload.
So the definition of WriteLine method and Console class all are already written and kept in the System namespace of the mscorlib assembly.
But my question is when the complier/CLR meets with the keywords like using,namespace, class,static, what does it do? Where is it written that it must treat the word next to the class keyword as a new type? Is it built in to the compiler/CLR? How does it work then?

C# keywords, which you can see the full list of here, are built into the CSC compller. When the compiler comes across any keyword, it is programmed to know what to expect and what to do.

This is part of the compiler, not the BCL. Check out the language specification which explains exactly what the compiler must do when it encounters any of the keywords.

Related

How is Complex number assignment from a double enabled? [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 6 years ago.
Improve this question
The Complex structure in System.Numerics allows assignment like this
Complex c = 3.72;
If I wanted to make my own Complex struct, how would I program this capability? I like it better than using constructors as in
Complex c = new Complex(3.72);
You need to declare an implicit conversion operator.
Thanks Code-Apprentice for the correct answer. I will just post exactly the code I added to my Complex struct
public static implicit operator Complex(double x)
{
return new Complex(x); // implicit conversion
}
This seems to work.
I will clarify why I am doing this. I wrote a large modeling code using Microsoft Complex everywhere. I tried to use the code in a Xamarin forms project but I could not get a reference to System.Numerics. To make progress I had to resurrect an old Complex struct I had made years ago, and get it to match the Microsoft capabilities.

C# checking if a word is in an English dictionary? [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 6 years ago.
Improve this question
I am trying to go through a list of words and for each one determine if it is a valid English word (for Scrabble). I'm not sure how to approach this, do I have to go find a text file of all English words and then use file reading and parsing methods to manually build a data structure like a trie or hashmap - or can I find those premade somewhere? What is the simplest way to go about this?
You can use NetSpell library for checking this. It can be installed through Nuget Console easily with the following command
PM> Install-Package NetSpell
Then, loop through the words and check them using the library
NetSpell.SpellChecker.Dictionary.WordDictionary oDict = new NetSpell.SpellChecker.Dictionary.WordDictionary();
oDict.DictionaryFile = "en-US.dic";
oDict.Initialize();
string wordToCheck = "door";
NetSpell.SpellChecker.Spelling oSpell = new NetSpell.SpellChecker.Spelling();
oSpell.Dictionary = oDict;
if(!oSpell.TestWord(wordToCheck))
{
//Word does not exist in dictionary
...
}
Since you're looking specifically for valid Scrabble words, there are a few APIs that validate words for Scrabble. If you use anything that's not for that intended purpose then it's likely going to leave out some words that are valid.
Here's one, here's another, and here's a separate question that lists available APIs.
So that I can add some value beyond just pasting links, I'd recommend wrapping this in your own interface so that you can swap these out in case one or another is unavailable (since they're all free services.)
public interface IScrabbleWordValidator
{
bool IsValidScrabbleWord(string word);
}
Make sure your code only depends on that interface, and then write implementations of it that call whatever APIs you use.

Is it possible to make a program that reads its own source code? [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 7 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What I mean is, could one possibly make a program that does the equivalent of
public class PrintOwnSourceCode
{
public static void Main ( )
{
System.Console.WriteLine([something]);
// prints "public class PrintOwnSourceCode { public static void Main ( ) { ... } }"
}
}
???
And would that be an example of reflection?
Somewhat.
Decompilers can do something similar to this:
I just decompiled a decompiler so I could use it to decompile itself
.NET Decompilers, like [.NET Reflector] (http://www.red-gate.com/products/dotnet-development/reflector/) and dotPeek are capable of reflecting upon a .NET assembly and generating files that resemble the source code. It will not look exactly like the source code because compiling and decompiling is kind of like translating English to French and then back to English--the results are not always guaranteed to be 1:1 as Google Translate can demonstrate. Information, like whitespace, that are for easy reading but not required by the compiler will be lost in the decompilation process. So, your application could decompile itself or invoke an external decompiler to print itself.
Aside
In compiled languages, the compiled code does not have direct access to the source code. (Companies don't typically ship the source code with the compiled code to customers. They only ship the compiled executable.) When it comes to parsed languages, like JavaScript, it's a whole different story. Because the source must be available to the runtime so that it can be parsed and run, the code can always find it's own source file, open it, and print it out.
This was answered here.
The short answer is that you cannot print it via reflection.
If you want to print out the file, then you will need to load in the source file itself (and have the file available).

What is the difference between console.writeline("hello world") and new WriteLine(){Text="Hello World"} [closed]

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 7 years ago.
Improve this question
I used to write
Console.WriteLine( "hello World" );
What is this one :
Is WriteLine is a method or a class? ( looks like it is a class)
this.Implementation = () => new Sequence
{
Activities = {
new WriteLine() {
Text = "Hello World" }
}
};
Assuming it compiles...
The first one invokes the WriteLine method of the System.Console class. This is the normal mechanism for writing text output to the console.
The second one is using a class called WriteLine and assigning a value to its Text property. The question is - what is this WriteLine class?
Assuming it's not your own invention, or part of some library you are using, perhaps it is the System.Activities.Statements.WriteLine class. You can check if you have a using statement at the top of your file that imports the System.Activities.Statements namespace. (You would also have had to reference the System.Activities.dll assembly.)
By itself, this will not actually do anything. The resulting class would have to be used in some other way to have any effect.
The System.Activities.Statements.WriteLine class and the assembly it belongs to are part of Windows Workflow Foundation.
See also, the answer to this StackOverflow question, which shows an example usage of the WriteLine class.

How do I create a method, and make a constructer that calls the method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
All in the Title.
Also please give an answer in less than a week.
I need someone to really help me with this, I'm using an online course and it says I have to Write the NumberBoard method, then write a NumberBoard constructer and call the method.
Here's the link to the .zip file that has the PDF telling me what to do, and the project that they give you:
https://d396qusza40orc.cloudfront.net/gameprogramming%2Frequired_assessment_materials%2FRequiredProjectMaterials.zip
(Copy and paste if link not clickable)
To get to the project, open the code folder and click the "GameProject" thing
Something like this will work
public class SomeClass
{
public SomeClass() //constructor
{
SomeMethod(); //Calling the method in constructor
}
public void SomeMethod() // Method
{
//Method implementation
}
}

Categories