Accepted style for long vs Int64 in C#? [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I know they are the same variable type, but is there an accepted standard 'style' for whether to use long or Int64?
I would like to use the most common one.

All documentation from Microsoft and 99.999% (or so) of the code you'll find online will use long. See for instance the numeric datatype definition in the C# reference: http://msdn.microsoft.com/en-us/library/exx3b86w.aspx
In the end, this is the same issue with using string or String. In general, lowercase names are used for value datatypes like numbers or characters, and uppercase names are used for classes. In C# Int64 is the complex datatype (a structure with fields, properties and methods, see the doc here) for the long value datatype (see the doc here). In general, people don't use the class Int64 for other than invoking methods from that class, such as Int64.Parse. So you will usually find something like this:
long variable = 9.5f;
string text = "8.4";
long anotherVariable = Int64.Parse(text);

You will get so many different opinions for a question like this since they're the exact same thing. So it's up to you. Microsoft pretty much always uses long, int and short in their documentation and examples. It's simply an Alias in VB.NET and C#. So I guess it's better usage to use them that way.
long instead of Int64
int instead of Int32
short instead of Int16

C# uses these keywords like long, int string, as aliases for .NET-types.
int = System.Int32
long = System.Int64
string = System.String
The first argument to use these keywords is that it comes with the language, so use it when writing the language.
A second argument of using these keywords is that you do not have to put using System; above your codefile.
Another benefit could be that someone could create a new type called Int64 and put it somewhere in your namespace (I know... it would be crazy). If your older code uses the type Int64, your older code could stop functioning. If your older code was using long (an alias for System.Int64) than it would still work fine.

You should use long, int and short.
The one exception I know is in a function name; look at BitConverter.ToInt64.
The reason for that is that long is not defined in CIL while int64 is.

I think it comes down to clarity versus compatibility. Int16, Int32 and Int64 are more clear than short, int and long. You know just by looking at Int16 that it is 16 bits in length.

I think in most cases it will come down to personal preference, but if you are strictly talking "C#" without specifying the underlying platform, I'd think a bit differently. If you are developing to adhere to C# standards for portability across platforms and "future-proofing", "long" would be the best way to go as it is an actual construct in the language itself. "Int64" is an actual struct (System.Int64) in the framework.

Related

Generating unique but readable names in C# [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am trying to write a service in which every user will be assigned a unique name, when he first uses the service. I wish to generate this name, rather than getting the user to set it up. Also, I want the name to be somewhat readable and memorable rather than sound like a GUID or a timestamp. Essentially I want this to be something like the Xbox gamertag.
I know that there will never be more than a 1000 users so maintaining the uniqueness would not be a problem (another reason why I can afford to avoid GUIDs)
I am thinking of taking some adjectives, nouns etc. from the dictionary and generating random but unique combinations of those.
Any suggestions?
You could use a corpus of English language n-grams (say of three letter sequences) and use them to generate words that look like English, but are actually completely gibberish. This kind of data is essentially random, but has a softness for the nature of human language and memory.
This is similar to what I'm talking about, except it combines entire words into sentences probabilistically. I was thinking more of doing it by composing letter sequences into imaginary words.
EDIT actually this page discusses what I'm talking about.
This is just a code example to fully approach your problem. In case it doesn't solve it, please try to be more specific in your question. Pass to the following method an instance of the System.Random class and a list of words (your dictionary).
static string GetGuid(Random random, IList<string> words)
{
const int minGuidSize = 10;
const int maxGuidSize = 15;
var builder = new StringBuilder();
while (builder.Length < minGuidSize)
builder.Append(words[random.Next(words.Count)]);
return builder.ToString(0, Math.Min(builder.Length, maxGuidSize));
}
You can use this list of 10 000 random name:
http://www.opensourcecf.com/1/2009/05/10000-Random-Names-Database.cfm
or use this website to generate a random list of firstname:
http://www.fakenamegenerator.com/order.php
Safe way. maintain the list of remaining non used names.
Easy way (also very scalable) but unsafe. Rely on the unlikelyhood that 2 users randomly get the same id.
I would try to get 3 or 4 lists of about a thousand modalities and then randomly picking one value in each list. That would make about 10E12 possibilities which is enough to avoid collision for 1000 users.
JohnLampMartin2212

What's the difference between data Validation and Verification? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
My recollection from a past employer is that they distinguished between the two as follows:
Validation is the process of checking that the data is appropriate in a very basic sense; for example that data in a date field can be converted to a date, or that characters in a number field can be converted to a number of the appropriate type;
Verification is the process of checking the typed data against some other 'business' rules that you impose on your interface - for example that the Date Of Birth field indicates an applicant within a certain age range.
These memories do not tie in with the Wikipedia article on the subject, nor a BBC BiteSize Revision article.
So what is the consensus: Do people care what methods and processes are called when I am checking Xml inputs for example?
What am I doing when I:
Check that a date field contains characters that are convertible to a C# DateTime;
Check that the DateTime is in an appropriate date range to be stored in SQL Server;
Check that the Date Of Birth indicates a customer over 18 but under 65?
In my vocabulary, validation is checking whether the format of the data is correct, IE if a you're actually dealing with a correctly formatted date string . Verification is checking whether the date you got is actually the date you were expecting.
Ok, so I'll take this as an open invitation to musing...
I think the difference is very much like compile-time vs. runtime errors. Just like the compiler is able to tell that two variables a,b are of type double, and thus the expression a/b is valid, it is only during runtime a DivideByZeroException may be raised if b turns out to be 0.
So to complete the analogy, one can validate that a string looks like a credit card number ('compile time'), but can only verify that it corresponds to a valid card only if one tries to charge the credit card ('runtime') with an amount
Duh. So I guess I understand it pretty much like you old company does.
in terms of programming it makes no difference what you call it (validation or verification) but where you put the logic is important. usually all three rules you mentioned are known to be validations with first two points corresponding to UI validation and last point to business rule validation. we usually validate UI fields using dataannotations in our controller and Business rule validation is performed within business layer. But the bottom line from software point of view is; don't do an operation (save, edit) unless data is good (you call it valid or verified).

Need a formula interpreter for .Net [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm looking for a formula interpreter that I can use in a C# application. It needs to be able to interpret a string like this:
max(1+2, 4) * x
I found Writing a fast formula interpreter (codeproject.com) which almost does what I need but it doesn't allow for functions with multiple parameters. I could probably add that functionality to it but I was just wondering if something like this already exists.
Thanks
A couple I've used in the past with no problems:
NCalc
Fast Lightweight Expression Evaluator
You can actually build a very effective interpreter by parsing and replacing certain functional keywords such as max with Math.Max and then dynamically building and executing the formula as a C# class and method. So actually you would be parsing and wrapping the formula and allowing the C# compiler to interpret and execute it.
So, taking your sample formula of max(1+2, 4) * x would turn into:
public class MyFormula
{
public double calc(double x)
{
return Math.Max(1+2, 4) * x;
}
}
Which you would compile on the fly and then execute per the linked article. You still have to parse for and pass the x value of course.
A long time ago in one project i had to create some booking with formulas, and i used VsaEngine. To use this engine you need to add reference to Microsoft.JScript. Here is example:
Code for usage is very simple, just replace formula parameters like:
string formula = "x+y";
formula= formula.Replace("x","100").Replace("y","200");
string result = CalculateFormula(formula);
And here is core method, CalculateFormula:
public string CalculateFormula(string evaluationString)
{
VsaEngine en = VsaEngine.CreateEngine();
Object result = Eval.JScriptEvaluate(evaluationString, en);
return result.ToString();
}
With this you can create your custom formula interpreter engine.

What is the difference between the type String and type string? (Uppercase first letter) [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C# what is the difference between String and string
I couldn't find the info anywhere, but I'm sure it's just a simple answer. Are they interchangeable??
Yes, they are identical. string is just the C# name for a System.String, which you can also use. See MSDN:
The string type represents a string of Unicode characters. string is an alias for System.String in the .NET Framework.
It's personal preference, but I always use string over String. I guess I like the blue color over the, uh, turquoise color offered by the default syntax highlighting.
You can see the other aliases under the C# value types on MSDN (e.g. int is really System.Int32 while long is really System.Int64).
You get this question a lot because in java there is a difference between for example Integer and int (pointer allocation). In C# there is no difference between String and string, nor there is a difference between Int32 and int etc..

What does the ? mean after a type? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
a curious c# syntax
So I've seen some code around and a few of them use a ? after the type, like this:
private Point? loc = null;
So I'm wondering if Point? is different than Point (can't put a question mark at the end of my sentence or I'll confuse you guys ... :] ). The language I'm using is C# by the way.
T? is a shorthand (in C#) for Nullable<T> - so Point? is another way of writing Nullable<Point> or example.
See sections 1.3 and 4.1 of the C# 3 language spec - and various other places, to be honest - for more details. See the docs for System.Nullable<T> for more information from the framework side of things. Or read chapter 4 of C# in Depth :) (Unfortunately it's not one of the free chapters.)
(This question is bound to be a duplicate, but I don't have the energy to find it right now.)
Point? is the same as Nullable<Point>. It allows you to assign null to value types, such as structs.
It means the type can accept its' value and null.

Categories

Resources