|
Generally, I'd say ignore websites - and particularly ignore Youtube tutorials - and get yourself a book (or better a course).
Both tend to be a lot better structured, introducing each subject in a streamlined way rather than dropping you in the deep end while covering all the material - a course is better because if you don't understand something you can talk to the tutor and he can explain it in different ways until you do.
Wrox, Addison Wesley, and Microsoft Press do some good ones - just start at the beginning and work your way through doing all the exercises. All of them should explain classes, methods, evente, and so on and why they are used.
But if you are proficient in VB versions above VB6, then the transition to C# shouldn't be too complicated: they both use the same .NET framework which you should be familiar with, and that means the learning curve is vastly reduced. And VB uses classes, methods, events - it's just the syntax which is rather better in C#!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you, much appreciated.
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
.NET Book Zero[^] is a great starting point, especially if you have some programming knowledge.
|
|
|
|
|
|
just tell me when we compare two string by == operation then does it compare by value or ref?
i read it https://stackoverflow.com/a/3678923
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/how-to-test-for-reference-equality-identity
but not very clear.
how to compare by ref and by value ?
i heard string intern. say if we have two string variable having same data then one string object is created and two string variable point to same memory address.
string strA = "Hello world!";
string strB = "Hello world!";
Console.WriteLine("ReferenceEquals(strA, strB) = {0}",
Object.ReferenceEquals(strA, strB));
when we check like this below way
if(strA==strB) then the comparision happen by value or by ref?
please guide me right info. thanks
|
|
|
|
|
Strings are reference types, but string comparisons are made as value types:
string a = "Hello World";
string b = "Hello";
string c = " World";
string d = b + c;
Console.WriteLine("{0}:{1}:{2}:{3}", a == d, a.Equals(d), (object)a == (object) d, object.ReferenceEquals(a, d));
Will give you:
True:True:False:False Because the first two tests use the string specific comparison, and the other two specifically do reference comparisons.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
With strings, there is another fact to consider. When the compiler can detect that they have the same value, the compiler will make sure that they have the same reference also. It can do that optimization because strings are immutable.
In the example given by OriginalGriff, the compiler did not detect that and thus the compiler created two different references.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
That's where it gets complicated (and why I didn't mention it - he is a beginner, after all).
The compiler can detect and intern strings, but the runtime doesn't unless you specifically ask it to:
string a = "Hello World";
string b = "Hello";
string c = " World";
string d = b + c;
Console.WriteLine("{0}:{1}:{2}:{3}", a == d, a.Equals(d), (object)a == (object)d, object.ReferenceEquals(a, d));
d = string.Intern(d);
Console.WriteLine("{0}:{1}:{2}:{3}", a == d, a.Equals(d), (object)a == (object)d, object.ReferenceEquals(a, d));
Will give you:
True:True:False:False
True:True:True:True As the "new" string is now in the Intern pool.
However ... in the release version of my original code you will probably get different results, as the optimiser will likely compact that code and intern the strings anyway!
There is a good description of the intern pool here: Understanding C#: String.Intern makes strings interesting - O'Reilly Broadcast[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
i have a very small question sir.
string a = "Hello World";
string b = "Hello World";
when we compare like if(a==b) then what will happen?
== operator compare by value or reference ?
when two string variable having same value and if == operator compare by vale then why value is checked by reference ?
please help me to understand this small area. thanks a lot in advance sir.
|
|
|
|
|
At this stage, I'd say "don't worry about it" - because it starts to get pretty complicated. When you write code like that, two additional factors come into play: optimisation and interning.
Optimisation is not normally applied to debug builds - they are optimised for "debuggability" not performance and that means that large sections of the compiler do not get used that can make significant changes to your code. For example, the compiler can look at this:
public bool IsSame()
{
string a = "Hello World";
string b = "Hello World";
return a == b;
} And "know" that it will always produce the same result: true. So the compiler can "throw away" the whole method and replace it with inline code for "true" wherever your code references it.
That makes it very difficult to say "it's this type of comparison" because the comparison may never occur at all, depending on what happens in the surrounding code! It doesn't do that for debug builds, because it makes it a lot harder for you the developer to work with in the debugger - you can't set a breakpoint on code which has been optimised out!
Interning is another complexity, and one I was hoping to avoid - and why I wrote my original code exactly the way I did. Interning basically means that because strings are immutable, the compiler "knows" that the two string literals are the same, so it only ever keeps one copy of it. So the comparison changes: it becomes a comparison between two identical references, so the question of "value or reference comparison?" becomes moot: they are the same. To see what I mean, try modifying my original code:
string a = "Hello World";
string b = "Hello";
string c = " World";
string d = b + c;
string e = "Hello World";
Console.WriteLine("{0}:{1}:{2}:{3}", a == d, a.Equals(d), (object)a == (object)d, object.ReferenceEquals(a, d));
Console.WriteLine("{0}:{1}:{2}:{3}", a == e, a.Equals(e), (object)a == (object)e, object.ReferenceEquals(a, e)); Run that, and you will get different results for the two WriteLines:
True:True:False:False
True:True:True:True The strings have been interned, so the references are the same.
If you want to know more - and this stuff can get quite complicated - I'd point you are the same link I did Bernhard: Understanding C#: String.Intern makes strings interesting - O'Reilly Broadcast[^] - but to be honest, this isn't stuff you need to know 99% of the time as it makes absolutely no different to how you code your app!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sir i was asked in interview that if we execute this code a==b then will it be compare by value or reference ? i said value and they told me nothing. i guess i was wrong.
so tell me when we compare a==b does this comparison occur by value or ref if two string variable having same data. thanks
|
|
|
|
|
If both variables are declared as string , then it compares by value.
Determines whether two specified strings have the same value.
public static bool operator == (String a, String b) {
return String.Equals(a, b);
}
public static bool Equals(String a, String b) {
if ((Object)a==(Object)b) {
return true;
}
if ((Object)a==null || (Object)b==null) {
return false;
}
if (a.Length != b.Length)
return false;
return EqualsHelper(a, b);
}
(Where EqualsHelper[^] essentially compares the strings character by character, but using unsafe code to improve the performance.)
If either variable is declared as object , then it will compare by reference.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
it seems to me that if check two string variable
String.Equals(a, b); OR a == b then it will be checking by value but if i check this way
if ((Object)a==(Object)b) then this type of checking is done by reference.
do i understand properly now ? am i right now ?
|
|
|
|
|
Yes, that's correct.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks you sir for your great help.
|
|
|
|
|
For a string, a == b is always a value comparison, but ... it starts with a reference comparison!
1) operator == calls string.Equals.
2) The first thing that Equals does is this:
if ((Object)a==(Object)b) return true;
Which is a reference comparison, as you'd expect. A good percentage of the time, this will make the process faster.
3) It then checks for null in either side, and returns false if either is null.
4) It then compares the lengths - any difference is a return false.
5) It then calls another method which repeats those two tests (if it's the debug build) and throws an exception via Contract.Requires
6) It then uses unsafe code with pointers to do a value comparison (but quickly, using 12 bytes at a time for AMD processors, and 10 for Intel - don't ask)
So technically, "a == b" is a value comparison, but it has a few additional tests to speed things up, including a reference comparison.
But you really aren't supposed to know that, the traditional answer is "value comparison for strings". It may be that they wanted you to expand on "value type" and maybe explain why it's not a reference comparison?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Wow, my reply in the interview would have been "I don't know, nor do I care". It must be a specialised requirement if this depth of knowledge is required for the position (I do LOB apps only).
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You should probably care about the value type comparison that is specific to strings - it does affect performance after all. And knowing that release optimisation could change how things work could be important.
But interning strings - particularly at run time - you don't normally need to know about. (The compile time interning is pretty much irrelevant as it's a "side effect" of immutability.)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Dear All,
I have an unmanaged DLL developed by Delphi language and it's support 86x and 64x and I want to use the DLL in my c# windows form application which use any cpu as a platform target but this error occurred
"System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at ScaleDLL.frmMain.AclasSDK_Initialize(Pointer Adjuct)
at ScaleDLL.frmMain.frmMain_Load(Object sender, EventArgs e) in F:\Dot Net Projects\Dlls\ScaleDLL\ScaleDLL\frmMain.cs:line 147"
and we can't change the platform target to 86x because we use other libraries and it is not work when change the platform and we need to use this dll on the AnyCpU platform.
Please help me ASAP.
Thank You
|
|
|
|
|
It's not simple, not at all.
Because you are using 64 bit libraries already, you can't access 32 bit libraries on the same process - even if you compile for "any cpu" that doesn't mean that a process can "switch" dynamically between the two.
If you want to cleanly use your legacy DLL, you will need to either convert everything else to x86, convert the legacy DLL to 64 bit, or ... start reading: Accessing 32-bit DLLs from 64-bit code | Reflections on IT[^] Do not expect this to be a clean, neat, and easy solution.
And no, I've never needed to do it - so I can't help you here...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Hi!how to create wordsearch in c#?
|
|
|
|
|
With classes, some variables, methods and a whole host of semi-colons. Hopefully you'll throw some algorithms in there as well.
This space for rent
|
|
|
|
|
|