Click here to Skip to main content
15,904,153 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Fabio Franco8-Jun-17 5:03
professionalFabio Franco8-Jun-17 5:03 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
TheGreatAndPowerfulOz8-Jun-17 5:22
TheGreatAndPowerfulOz8-Jun-17 5:22 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Member 102778078-Jun-17 6:14
Member 102778078-Jun-17 6:14 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Fabio Franco8-Jun-17 6:57
professionalFabio Franco8-Jun-17 6:57 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
pherschel7-Jun-17 6:12
pherschel7-Jun-17 6:12 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
TheGreatAndPowerfulOz7-Jun-17 6:57
TheGreatAndPowerfulOz7-Jun-17 6:57 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Fabio Franco8-Jun-17 1:55
professionalFabio Franco8-Jun-17 1:55 
GeneralRe: c# Const vs. Readonly, Scope, Usings PinPopular
F-ES Sitecore7-Jun-17 5:18
professionalF-ES Sitecore7-Jun-17 5:18 
pherschel wrote:
Is it me or do you get confused by this? Why can't I say
C#
const DateTime today = DateTime.Now;


Constants aren't variables, they just look like them, they are aids for the compiler, they don't exist at run-time. When you write

C#
const int x = 5;
int y = 2;
int z = y + x;
bool b = z <= x;


what gets compiled is this
C#
int y = 2;
int z = y + 5;
bool b = z <= 5;


The compiler replaces all instances of "x" with the constant value. If you could define x as DateTime.Now then what would be compiled? If it literally replaced "x" with "DateTime.Now" everywhere it appears then you almost certainly would not get the result you desire. If it replaced DateTime.Now with the date of compilation then that wouldn't work either. What you really want is a read-only variable, and that's why we have read only variables and constants. You have to understand what they are and use them appropriately, that's not a failing of .net.

This is also why you can't make objects const.
C#
const Person p = new Person();
p.FirstName = "John";
p.Age = 33;
Console.WriteLine (p); // what will the compiler replace "p" with?


pherschel wrote:
For properties, why can't I hide the worker variable for the rest of the class?


Use the short-hand version

C#
int PageNbr { get; set; }


Again it's simply a case of understanding the difference and knowing when and where to use the appropriate solution.

pherschel wrote:
For destructors, why can't you give me option to destroy right away?


Because .net is better at memory management than you are and if you leave .net to do your memory management you'll get code that performs well, if you try and do your own memory management you get code that performs badly. For one it means the code is harder for the compiler to optimise as the compiler likes to move your code around for the best overall result but when you have in-line memory management you lesser the optimiser's abilities. Also memory deallocation is expensive. If you could free your objects immediately you probably would (otherwise why would you want this feature?) and that will result in a not-insignificant performance hit. By leaving this aspect to .net it can clear the memory down at a time better suited, like when your app is idling, or some other conditions are met. That way the performance hit is not noticeable, it discretely happens in the background.


pherschel wrote:
If you open a file and a DB you have to nest usings before you even get started doing some work


You don't have to nest usings at all

C#
using (This x = new This())
using (That y = new That())
{
   // code here
}

GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Foothill7-Jun-17 8:50
professionalFoothill7-Jun-17 8:50 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
kalberts8-Jun-17 1:54
kalberts8-Jun-17 1:54 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
dandy727-Jun-17 5:21
dandy727-Jun-17 5:21 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
TheGreatAndPowerfulOz7-Jun-17 5:46
TheGreatAndPowerfulOz7-Jun-17 5:46 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
dandy728-Jun-17 3:45
dandy728-Jun-17 3:45 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
kalberts8-Jun-17 2:21
kalberts8-Jun-17 2:21 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
dandy728-Jun-17 3:44
dandy728-Jun-17 3:44 
GeneralRe: c# Usings Pin
irneb8-Jun-17 5:07
irneb8-Jun-17 5:07 
GeneralRe: c# Const vs. Readonly Pin
irneb8-Jun-17 5:14
irneb8-Jun-17 5:14 
GeneralRe: c# Const parameters Pin
irneb8-Jun-17 5:27
irneb8-Jun-17 5:27 
GeneralRe: c# Scope in properties Pin
irneb8-Jun-17 5:30
irneb8-Jun-17 5:30 
GeneralRe: c# Const vs. Readonly, Scope, Usings Pin
Caslen9-Jun-17 8:36
Caslen9-Jun-17 8:36 
GeneraliOS 11 can automatically delete apps to save space Pin
ZurdoDev7-Jun-17 3:24
professionalZurdoDev7-Jun-17 3:24 
GeneralRe: iOS 11 can automatically delete apps to save space Pin
harold aptroot7-Jun-17 3:35
harold aptroot7-Jun-17 3:35 
GeneralRe: iOS 11 can automatically delete apps to save space Pin
lopatir7-Jun-17 3:37
lopatir7-Jun-17 3:37 
GeneralRe: iOS 11 can automatically delete apps to save space Pin
ZurdoDev7-Jun-17 3:39
professionalZurdoDev7-Jun-17 3:39 
GeneralRe: iOS 11 can automatically delete apps to save space Pin
  Forogar  7-Jun-17 3:47
professional  Forogar  7-Jun-17 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.