var keyword constraints as class fields in C#

Hello friends. Hope you are doing well!!

Wish you happy new year!!

I am here to talk about the var keyword constraint, when using it in the class level fields in C#. Sometimes , we get a question that why can’t we use var keyword in initializing the class level fields. Let’s have a look at the code snippet, given below: 

  1. class A  
  2. {  
  3.  var x; //error  
  4.  int y; //ok  
  5. }   

The above declaration of x is not right as var is not allowed to be used in the class level declaration and this is the reason, it throws the following error. 

“The contextual keyword ‘var’ may only appear within a local variable declaration or in script code.”

Clearly var is not something that can replace the data types. But why so? This is because the compiler is designed in a way that it assumes that all the class level initialization will have their type defined already. If you are interested in the detailed answer, you may want to read the post from Eric Lippert at the link:

 If you have gone through the link, mentioned above, you can understand that in order to support the var in the class fields, re-write of the compiler is required and this would be a huge change. Until that happens, keep using the var inside the methods, where it can infer the datatype at the compile time.

Hope you liked the post. Look forward to your comments.

Enforcing date pattern with DateTime.TryParseExact method

Hello friends,

Today we will go though a useful method of datetime class called TryParseExact.  You can use this method to specify the pattern in which you want user to enter the dates. There are two overloads as following.

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)
TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)

Example: var isValid = DateTime.TryParseExact(Convert.ToString(value), “d MMM yyyy”, CultureInfo.CurrentCulture,
DateTimeStyles.None, out dateTime);

Here if user enter date like 1-Jan-2018, if will be true else false. Please note that 
MMM is case sensitive and mmm will not work.

Hope you found this post useful. Please share your comments.

Constraint with properties in passing as reference

Hello friends,

I happened to encounter this trouble while trying to pass the reference of a Property.

public class LinkedListQ : QNode

{

private QNode head = null;

public QNode Head

{

get

{

return head;

}

}

class Program

{

static void Main(string[] args)

{

LinkedListQ que = new LinkedListQ();

que.ReverseList(ref que.Head); //Error “A property, indexer or dynamic member access may not be passed as an out or ref parameter”

}

The reason as per MSDN is – Properties are not variables. They are methods, and cannot be passed to ref parameters.

The workaround I figured out is to change the head variable to internal in order to be accessible from the Program class and pass the head variable reference instead of the Property.

internal QNode head = null;

que.ReverseList(ref que.head); //Works fine

Hope you liked the post. Please share your comments.
Reference: C# Reference