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

КОМЕНТАРІ • 57

  • @questpondvideos
    @questpondvideos  3 роки тому

    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

  • @zerosandones7547
    @zerosandones7547 3 роки тому +15

    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)

  • @klocugh12
    @klocugh12 2 роки тому +9

    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!

  • @dnskk
    @dnskk 8 років тому +18

    sir const are implicitly static so u need not to put static in front of them

  • @expertreviews1112
    @expertreviews1112 8 років тому +7

    absolutely fantastic video. goodness me such good explanation. Be Blessed

  •  10 років тому +1

    Very helpful. Thank you. Never really understood the point of readonly until now.

  • @marklitvin9291
    @marklitvin9291 7 років тому +2

    Very well explained. Thank you!

  • @HikeThePlanets
    @HikeThePlanets 4 роки тому +1

    Thanks so much for explaining that so simply and thoroughly.

  • @olglez9230
    @olglez9230 3 роки тому

    Nice one. Very clearly explained with a good example. This may come up on an interview open day!

  • @faisalraufpitafi1396
    @faisalraufpitafi1396 6 років тому +1

    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 .

  • @saurabhmahal1354
    @saurabhmahal1354 11 років тому

    Great way to tell the difference ...thanks

  • @pcm0it
    @pcm0it 11 років тому +8

    The static modifier is not allowed in a constant declaration.

  • @nocultist7050
    @nocultist7050 3 роки тому

    This is a hidden gem of a channel.

  • @mradosovic
    @mradosovic 3 роки тому

    Very nice explanation with a good example.

  • @amolshinde7832
    @amolshinde7832 3 роки тому

    Very good explanation, thanks

  • @harshsaxena05
    @harshsaxena05 2 роки тому

    excellent explanation thank you.

  • @Albileon
    @Albileon 11 років тому +1

    Really good, thanks.

  • @YourSacredPath
    @YourSacredPath 22 дні тому

    Thanks for sharing this informatational video

  • @manjeetnandal1
    @manjeetnandal1 4 роки тому

    Great sharing!
    Thanks....

  • @venkateshpillay3213
    @venkateshpillay3213 2 роки тому

    Great sir 👌 Thank you

  • @prateekbhardwaj9943
    @prateekbhardwaj9943 7 років тому

    nicely explained

  • @aks-tech-1
    @aks-tech-1 6 років тому

    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

  • @kavithan1987
    @kavithan1987 5 років тому

    Instead of read-only we can use integer right..is their any difference in memory allocation

  • @MrJonnis13
    @MrJonnis13 7 років тому

    Thank you

  • @sery152
    @sery152 4 роки тому

    Can you explain the logic of static inline with const and readonly, they have contradictins...

  • @yeshyuvi
    @yeshyuvi 12 років тому

    its good and thank u

  • @smanputra
    @smanputra 11 років тому

    great explanation and examples..

  • @pankajjha1914
    @pankajjha1914 4 роки тому

    thnku sir.

  • @codewithparveenyadav
    @codewithparveenyadav 6 років тому

    useful

  • @semikolon4229
    @semikolon4229 6 років тому

    thanks, very useful

  • @rajarajanseeman6758
    @rajarajanseeman6758 8 років тому +2

    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

    • @arageshashi
      @arageshashi 7 років тому

      Seems that code is written in text editor not in any fiddler or vs ide.

    • @jvsnyc
      @jvsnyc 5 років тому

      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++.

  • @Shakti_Singh_Om
    @Shakti_Singh_Om 2 роки тому

    Important point here is that constants are by default static. You can use static keyword with Readonly and not with constants.

  • @naterdanger3919
    @naterdanger3919 6 років тому

    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?

    • @jvsnyc
      @jvsnyc 5 років тому

      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.

  • @ahmadezzat9031
    @ahmadezzat9031 6 років тому +3

    The const is by default static.

  • @questpondvideos
    @questpondvideos  4 роки тому

    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

  • @sandeepadode1
    @sandeepadode1 6 років тому

    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?

    • @jvsnyc
      @jvsnyc 5 років тому

      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.

  • @rohitjoshi3240
    @rohitjoshi3240 3 роки тому

    You declare the static Constructor and used the access modifier, which prone to error.

  • @beingshuklaji
    @beingshuklaji 7 років тому +2

    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 :)

    • @arageshashi
      @arageshashi 7 років тому

      Seems that code is written in text editor not in any fiddler or vs ide.

    • @beingshuklaji
      @beingshuklaji 7 років тому

      But while. Running the application how it is showing the correct output.

    • @tusharghude15
      @tusharghude15 7 років тому +5

      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

    • @vatansoni6439
      @vatansoni6439 7 років тому

      +tushar ghude good

    • @dineshkumarachari8757
      @dineshkumarachari8757 Рік тому

      @@tusharghude15 correct! but he not mention that point is important point.

  • @zerosandones7547
    @zerosandones7547 3 роки тому

    static constructor in IL code is cctor

  • @ankitsinha007
    @ankitsinha007 2 роки тому

    A static constructor does not have access modifier

  • @PanikGrafik
    @PanikGrafik 2 роки тому

    Dont forget that the value IS STILL HELD !!!

  • @hlch7774
    @hlch7774 Рік тому

    const no need to use static

  • @williamwang2047
    @williamwang2047 7 місяців тому

    i think u said wrong, PI should be const, as it never changed......

  • @lakshmisruthi1342
    @lakshmisruthi1342 2 роки тому

    I wish I watched this video before attending interview :/

  • @wiepcorbier
    @wiepcorbier 5 років тому

    Stop the "you know" please.

  • @naturebc
    @naturebc Рік тому

    This is one of those things that makes a language worse. More complex for no good reason at all.