Posts

Showing posts with the label C#

Writing and Reading Text File using C#

Introduction Reading and Writing to text file is common requirement of programmers. C# provides a convenient way to work with text files. .NET Framework includes convenience classes that make reading and writing text files very easy. Actually, to write (create) or read files, there is three certain steps we need to follow. (i) Opening the file (ii) Read or Write (create new file) file (iii) Close the file The best way to understand working with files is to jump right into the code, let's jump. Writing File using System; using System.IO; class WritingFile {     static void Main( string [] args)     {         //opening a file stream to write text         TextWriter tW = new StreamWriter ( "page.txt" );         //write the line to file         tW.WriteLine( "text goes here" );         //close the stream         tW.Close();     } } In above code, 'System' namespace is base or say primary

Checked and Unchecked Code in C#

Image
Introduction C# has huge list of benefits, checked and unchecked code writing is one of them. Lets talk on some real example. int val1 = 2147483647; int val2 = val1 + 1; Console .WriteLine( "Value1 is {0}" , val1); Console .WriteLine( "Value2 is {0}" , val2); Above example has a variable val1 which has value 2147483647 which is largest value of integer. In second line of code, we have val2 and we are trying to add 1 to existing largest value of integer. Above code will output exactly as following code: int val1 = 2147483647; int val2; unchecked {     val2 = val1 + 1; } Console .WriteLine( "Value1 is {0}" , val1); Console .WriteLine( "Value2 is {0}" , val2); In above both terms C# rolls the value if integer range exceeds so, we are getting output as following. Actually adding 1 in 2147483647 should be 2147483648 but our value is rolling because it exceeding the integer range. So, here if you do wi

Destructors in C#

Introduction Before reading this post I will recommend you to read Constructors in C# post. Dot Net (.NET) framework has an in built mechanism known as Garbage Collection to deallocate memory occupied by the unused objects. In other word, destructor is something like method and constructor except that the runtime calls it after the last reference to object has disappeared. A destructor same name as the name of the class but it starts with the character ~. using System; class ClassRoom {     private int boycount;       //field     public ClassRoom()          //default constructor     {         boycount = 30;     }     ~ ClassRoom()     {         boycount = 0;     }     static void Main( string [] args)     {         ClassRoom r;                //creating a class variable, object         r = new ClassRoom ();        //initializing class variable                                     //or direct use "

Partial Classes in C#

Introduction Partial class is one that can be split up across multiple physical files. This feature is introduced with the release of C# 2.0 versions. With C#, we can split the source code for a class into separate files so that we can organize the definition of a large class into smaller, easier to manage pieces. When we split a class across multiple files, we define the parts of the class by using the partial keyword in each file. Each file contains a section of the class definition, and all parts are combined when the application is compiled. Look at the example below: Original Long Class File (Calculation.cs) class ClassRoom {     private int boycount;   //field     public ClassRoom()     //default constructor     {         boycount = 30;     }     public ClassRoom(int bcount)     //overloaded constructor     {         boycount = bcount;     }     public double Avg()     //method     {         //statements goes here     } } Splitte

Using while Statements in C#

We use a while statement to run a statement repeatedly while some condition is true.   Syntax:- while ( booleanExpression )       statements The Boolean expression is evaluated, and if it is true, the statement runs and then the Boolean expression is evaluated again. If the expression is still true, the statement is repeated and then the Boolean expression is evaluated again. This process continues until the Boolean expression evaluates to false, when the while statement exits. Execution then continues with the frst statement after the while statement. A while statement shares many syntactic similarities with an if statement (in fact, the syntax is identical except for the keyword): * The expression must be a Boolean expression. * The Boolean expression must be written inside parentheses. * If the Boolean expression evaluates to false when frst evaluated, the statement does not run. * If we want to perform two or more statements under the control of a while s

Namespaces in C#

Introduction Namespaces are C# program elements that allow us to create a system to organize our code. One more very important use, it avoids name clashes between two sets of code. Using namespaces in our code is a good habit because it is likely to save us from problems later when we want to reuse some of our code. In this post we will talk about namespaces: (i) delimited by . (dot) operator and (ii) by using 'using' directive Let's look at example, in console application to print/write line on console we uses 'Console.WriteLine("Hello");'. This is only possible, if you use 'using System' directive in code. What is 'System' here? That's the namespace. Let's look at the ways to use namespaces in program. (i) Dot Operator class Program {     static void Main( string [] args)     {         System. Console .WriteLine( "Using dot operator." );         System. Console .ReadKey();     } }

Strings in C# - Part 4

Introduction MSDN Reference Well, you need to learn Part 1 of this post to understand this better. In the previous posts we have tried to change the content of strings using various methods like Replace(), ToUpper() etc. But if you recall, value of a string cannot be modified once it is established. The methods like Replace() may seems to change the content of the string but actually, those methods just output a copy of the string and original string remains same. Look at example: using  System; using  System.Collections.Generic; using  System.Linq; namespace  ConsoleApplication1 {      class   Program     {          static   void  Main( string [] args)         {              string  str1 =  "Hello Abhimanyu" ;              Console .WriteLine(str1);              string  str2 = str1.ToUpper();              Console .WriteLine(str2);              //Let's see original string variable              Console .WriteLine(s

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples