Difference Between Constant and ReadOnly in C# | CSharp Interview Questions & Answers
Вставка
- Опубліковано 4 лип 2024
- For more such videos visit www.questpond.com
For more such videos subscribe ua-cam.com/users/questpondvide...
See our other Step by Step video series below :-
For more such videos subscribe / questpondvideos
See our other Step by Step video series below :-
JavaScript Interview Questions: • JavaScript Interview Q...
C# Interview Questions:- • C# Interview Questions...
Abstract class vs Interface in C# :- • Abstract Class vs Inte...
C# tutorial for beginners(4 hrs):- • C# Tutorial for Beginn...
Learn Azure Step by Step:- • Azure Tutorial for Beg...
Azure AZ-900 fundamentals certification :- • AZ 900 Certification |...
Angular Interview Questions and Answers :- • Angular Interview Ques...
Learn Angular tutorial step by step tinyurl.com/ycd9j895
Learn MVC 5 step by step in 16 hours:- • Learn ASP.NET MVC 5 ( ...
Learn Design Pattern Step by Step goo.gl/eJdn0m
Learn MSBI Step by Step in 32 hours:- goo.gl/TTpFZN
Learn SQL Server Step by Step tinyurl.com/ja4zmwu
Python Tutorial for Beginners:- • Python Tutorial for Be...
Learn Data Science in 1 hour :- tinyurl.com/y5o7qbau
Learn Power BI Step by Step:- tinyurl.com/y6thhkxw
PHP Interview Questions : • PHP Interview Question...
Learn Tableau step by step :- tinyurl.com/kh6ojyo
Preparing for C# / .NET interviews start here • C# Interview Questions...
In this video we will try to understand what is the difference between 2 important keywords in c# Constant and readonly.
We are also distributing a 100 page Ebook ".NET Interview Question and Answers". If you want this ebook please share this video in your facebook/twitter/linkedin account and email us on questpond@questpond.com with the shared link and we will email you the PDF.
-----------------------------------------------------------------------------------------------
For more details :-
🌐 Website : www.questpond.com
📱 Mob. No. : +91-9967590707 / +91 7700975156 / +91-22-49786776
📧Email : questpond@questpond.com / questpond@gmail.com / questpond@yahoo.com
Share, Support, Subscribe and Connect us on!!!
UA-cam: / questpondvideos
Twitter: / questpond
Instagram : / questpond
Facebook : / questpond
#ConstantvsReadOnly #CSharpConstant #C#ReadOnly #C#Concepts #CSharpInterviewQuestions #C#InterviewQuestions&Answers
ASP.NET, C# Job Market : ua-cam.com/video/H3JEWAp32-s/v-deo.html
30 Important C# Interview Questions : ua-cam.com/video/BKynEBPqiIM/v-deo.html
25+ OOPS Interview Questions : ua-cam.com/video/u99wAoBjDvQ/v-deo.html
Software Architecture Interview Questions : ua-cam.com/video/AtTgcbLOqMM/v-deo.html
20+ SQL Server Interview Questions : ua-cam.com/video/SEdAF8mSKS4/v-deo.html
10+ Power BI Interview Questions : ua-cam.com/video/Cozc9WNBRt4/v-deo.html
20 MSBI Interview Questions : ua-cam.com/video/Nw_sHEKnOUE/v-deo.html
SQL Server Joins : ua-cam.com/video/KTvYHEntvn8/v-deo.html
SQL Step by Step - ua-cam.com/video/uGlfP9o7kmY/v-deo.html
Angular Step by Step Tutorial for Beginners : ua-cam.com/video/-9VcW7MBDs8/v-deo.html
25 Angular Interview Questions : ua-cam.com/video/-jeoyDJDsSM/v-deo.html
25 Important ASP.NET Interview Questions : ua-cam.com/video/pXmMdmJUC0g/v-deo.html
35 Important JavaScript Interview Questions : ua-cam.com/video/Zb4dPi7CANU/v-deo.html
20 MySQL Interview Questions : ua-cam.com/video/9hfjC-BpY20/v-deo.html
5 MSBI Interview Questions : ua-cam.com/video/5E815aXAwYQ/v-deo.html
20 PHP Interview Questions : ua-cam.com/video/1bpNSynUrl8/v-deo.html
Constants:
Constants based on "consts":
- const fields are called compile-time constants
- an absolute constant
- the value of a const variable is set during the declaration itself
- consts variables SHOULD have a value when declared
- the const's value is stored in the IL code itself
Constants based on "readonly":
- the modifier readonly creates fields, which values cannot be changed once they are assigned.
- don't need to define a value during the declaration itself
(a readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which
the field is defined or a variable initializer))
- readonly fields are called run-time constants
- their values cannot be changed after assignment
- this process(value assignment) happens during the execution of the program (in runtime)
You didn't mention one thing about const:
Compiler actually writes literal value of const as is when compiling. So there is no variable in memory, just a literal 100 number. It is important when you use it in other assemblies. When you modify it where it's defined, other assemblies will still hold value it had when it was compiled, so they have to be recompiled again!
sir const are implicitly static so u need not to put static in front of them
absolutely fantastic video. goodness me such good explanation. Be Blessed
Very helpful. Thank you. Never really understood the point of readonly until now.
Very well explained. Thank you!
Thanks so much for explaining that so simply and thoroughly.
Nice one. Very clearly explained with a good example. This may come up on an interview open day!
sir Aslam o alikum I am from Karachi Pakistan I like your teaching way Mashallah Mashallah you are perfect teacher and I will share your videos with my friends because I want they should learn .
Great way to tell the difference ...thanks
The static modifier is not allowed in a constant declaration.
This is a hidden gem of a channel.
Very nice explanation with a good example.
Very good explanation, thanks
excellent explanation thank you.
Really good, thanks.
Thanks for sharing this informatational video
Great sharing!
Thanks....
Great sir 👌 Thank you
nicely explained
I have one suggestion here. You can brief the topic and then explain the programming. You are good in all the way. I am your paid user
Instead of read-only we can use integer right..is their any difference in memory allocation
Thank you
Can you explain the logic of static inline with const and readonly, they have contradictins...
its good and thank u
great explanation and examples..
thnku sir.
useful
thanks, very useful
Still wondering how would you be able to mark as static in const variable? I see the below error
Error 1 The constant 'xxx.Program.myproconst' cannot be marked static
Seems that code is written in text editor not in any fiddler or vs ide.
If it is not clear to anyone, that's right, it can't. Doubly confusing for C++ programmers coming over to C# because in C++ const has a different meaning and does not automatically imply static. In C# it appears const implies static and you can't say both just for fun or to be more clear. In C++ the same thing would be static const, because if you said just const you would get one for each object instance. C# has a readonly keyword that doesn't exist in C++.
Important point here is that constants are by default static. You can use static keyword with Readonly and not with constants.
Forgive me if this was asked already, but is it necessary to label it constant to begin with? Why/When would we need to do such a thing?
It is to protect you against goofy mistakes by yourself or others on your team. If something is logically a compile-time constant (or even readonly) if you try to change it later by mistake you can't. There are also optimizations that can be applied to values known not to change, but that is secondary.
The const is by default static.
New Announcement from Questpond ".NET(C#) FUNDAMENTALS, .NET COLLECTIONS, GENERICS" Course now available @ 799 /12 $. If Interested visit this Link for Subscription with Questpond
www.questpond.com/netc-fundamentals-net-collections-generics/cid22
My question is why to read only?A constant is constant right be it at compile time or at run time.Does it have any impact on the program?please help.What exactly is the use of using anyone among them?
It is to protect you against goofy mistakes by yourself or others on your team. If something is logically a compile-time constant (or even readonly) if you try to change it later by mistake you can't. There are also optimizations that can be applied to values known not to change, but that is secondary.
You declare the static Constructor and used the access modifier, which prone to error.
Hello Sir ji .. Its really nice example but i have 1 question here that we can't have static and const simultaneously ?? it is saying that you cant have static with the const ? Could you please let me know how this happens in your example ?? Thanks you so much for your help :)
Seems that code is written in text editor not in any fiddler or vs ide.
But while. Running the application how it is showing the correct output.
watch carefully he changed it (at 05:48 u can see suddenly public and static disappeared)
ur rgt constants cannot be declared as static coz they are implicitly static
u always use const like classname.const_var
+tushar ghude good
@@tusharghude15 correct! but he not mention that point is important point.
static constructor in IL code is cctor
A static constructor does not have access modifier
Dont forget that the value IS STILL HELD !!!
const no need to use static
i think u said wrong, PI should be const, as it never changed......
I wish I watched this video before attending interview :/
Stop the "you know" please.
This is one of those things that makes a language worse. More complex for no good reason at all.