C# - Deallocating Memory, Static Class Destructor?
Jan 13, 2011
I have a static class I'm using as my data layer in my website. In this class, I have string arrays that store queried information that I can access later. Here's the part of my class and the method in question:
public static class data_layer
{
ivate static string[] items;
private static string[] description;
//will return description for an item id. if no item id is found, null is returned
public static string getDesc(string key)
[code]....
If my class has NO destructor, an it goes out of scope. GC runs at certain time, now will it simply reclaim memory from my class OR will it call its destructor or Finalize () on it? And does the .net framework class like SQLConnection implement a destructor? I saw it has a Dispose () implementation but didn't see the destructor using "Go to definition".
I have a private static field in my Controller class in an MVC web application.
I have a static method in that controller that assigns some value to that static field, I want to apply lock on that static field until some other instance method in the controller uses the value stored in the static field and then releases it.
DETAILS:
I have a controller named BaseController having a static ClientId field as follows and two methods as follows:-
public static string ClientId = ""; static void OnClientConnected(string clientId, ref Dictionary<string, object> list) { list.Add("a", "b"); // I want the ClientId to be locked here, so that it can not be accessed by other requests coming to the server and wait for ClientId to be released:- BaseController.clientId = clientId; } public ActionResult Handler() { if (something) { // use the static ClientId here } // Release the ClientId here, so it can now be used by other web requests coming to the server. return View(); }
I have a question about C Sharp ASP.NET:Is there a difference (in code speed, resources) between:public static variable declared in public static class MyGlobals in a 'Code File' template;and the variable declared in a normal 'Class File' template;I use this variable in 2 different Class Files and also in _Default Page codebehind cs file.In fact in my case I need about 20 global variables of type List<string>.
I need an advice on piece of functionality that I am ought to implement. The scenario is that we haven an HttpHandler which servers to intercept file uploads. In the handler, I need to persist a large dictionary of strings inside the memory. The dictionary might be as large as 100 entries. I am wondering whether it is safe to store that in a static variable, so that it is not initialized every time instance of the handler is created (there will be a lot of instance for sure). In general, what is the approach in such scenarios. Is it a generally better idea to use static fields, to persist data that will not be changed?
I am getting some memory issues, not entirely sure but does anyone think this could be the issue.On the itemdatabound event the following code runs, and it calls a shared class method Return Image which resizes the original image. Could the coding be the issue for the memory problem. There are about 10 or so items.
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
If a static class is put in App_Code, does that imply only one instance will be created, shared by different http requests?Or each request still brings one request?
is it ok to use something like this in the web: (my application is on asp.net mvc)
public static class DbUtil { public static int Insert(object o, string cs) { using (var conn = new SqlConnection(cs)) using (var cmd = conn.CreateCommand()) { conn.Open(); return Convert.ToInt32(cmd.ExecuteScalar());
So i have some existing functionality from an older ASP.Net app that is a static class.
Say there's a static method in that class that returns a dataset that I would like to bind to something like a gridview or whatever I can build using MVC3.
I am using <%# MyFormatClass(Eval("fieldname")) %> to display data in a grid view on my page. MyFormatClass works fine, but now I want to move it to a central location that can be used by many pages. When I try to access the class <%# Utils.MyFormatClass(Eval("fieldname")) %> it no longer works. Is this something we are allowed to do?
Since I'm using Postgresql and can't use LINQ to SQL, I wrote my own wrapper classes.
This is a part of the Student class:
[code]....
It works now! I replaces all Run methods in the Student class with DB.Run
But I want to know if it will work fine with a lot of people online, not me only. I'm not sure how static things work with ASP.NET, maybe it'll eat a lot of memory?..
I would like to generate my links automatically via a static class in each of my aspx pages (or in a common BasePage).Currently I use this:
private const string TEMPLATE = "~/One.aspx"; public static string Link () ( string link = String.Format(TEMPLATE); return link; )
But the name of my page, One.aspx is hardcoded. Is it possible to generate the path instead of this hardcoded constant TEMPLATE. You should know that I do not instantiate the class before creating the link.
I have a method that returns an array of custom class objects that are created by parsing a text file. At the moment every time I use it I am rereading the file which isn't very efficient.
What I wat to do is create an array containing the objects when the page loads and store them in an array which can then be used later.
[Code]....
I thought I could create the static object using something like:
static Album myAlbums[] = readArray("Albums.txt");
but I am getting the following error:
A field initializer cannot reference the nonstatic field, method, or property 'B2M._Default.readArray(string)'
I am new to C# so this is probably something dumb. (Feel free to poke fun in my general direction if this is the case!)
I have some confusion with singleton class, below are my some points:
1.Can singleton class have static method?,if yes then how we call that methods? 2.what is main difference between Static class and Singleton Class?
I have created my singleton class as follows:
[Code]....
In Above class structure I have created two method one is Static and second is non static, When I am trying to access Static Method it gives me compile time error.
I hate to see the name of the class used as a string parameter like "FileDownloader" in the code, and I would like to use something like this FileDownloader.Name(), where FileDownloader is name of the class. Only problem is that I can't find out how to do that without instantiating object or creating a static method...
Is there a way to get a class name in .net without having the object instance and without creating a static method that returns the name of the class?
What is the use of declaring the static class. I know that we cant create the object for the static class but i want to know that is the use of creating the class as static.
I understand the problem of the static variable inside a class in the web application, now I try to create a simple class to deal with SQL Server for example, so I created Static Method inside this class, this static method used in insert, update and delete from database, I wonder if this is safe, and there is no problems will appear ?
We have a Static class that 's called CData in our asp.net c# app.It handles all the data layer CRUD functionality on our pages.
Problem is, were are losing the intellisense reference in our pages to it.For example: we would expect to type CData.(dot) and have a list of methods and properties available to us.
Nope. Not there.
I kind of thought it was because the file was getting too long at 3000 lines of code, since some other static classes were working ok.
So then I thought that I'd try wrapping the CData Class in a namespace. That worked, but of course I had to put an include statement of the top of each page that's using CDATA, also, now some of the other developers are getting errors in there pages complaining about missing methods that are in CDATA, but their pages are not seeing.